Data Structure Binary Tree: Construct Full Binary Tree from given preorder and postorder traversals
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <fstream>
#include <map>
using namespace std; struct node {
int data;
struct node *left, *right;
node() : data(), left(NULL), right(NULL) { }
node(int d) : data(d), left(NULL), right(NULL) { }
}; void prints(node *root) {
if (!root) return;
prints(root->left);
cout << root->data << " ";
prints(root->right);
} node *_buildtree(int pre[], int post[], int &preindex, int l, int h, int size) {
if (preindex >= size || l > h) return NULL;
node *root = new node(pre[preindex++]);
if (l == h) return root;
int i = l;
for (; i <= h; i++) if (pre[preindex] == post[i]) break;
if (i <= h) {
root->left = _buildtree(pre, post, preindex, l, i, size);
root->right = _buildtree(pre, post, preindex, i+, h, size);
}
return root;
} node *buildtree(int pre[], int post[], int size) {
int preindex = ;
return _buildtree(pre, post, preindex, , size-, size);
} int main() {
int pre[] = {, , , , , , , , };
int post[] = {, , , , , , , , };
node *root = buildtree(pre, post, );
prints(root);
return ;
}
Data Structure Binary Tree: Construct Full Binary Tree from given preorder and postorder traversals的更多相关文章
- [Swift]LeetCode889. 根据前序和后序遍历构造二叉树 | Construct Binary Tree from Preorder and Postorder Traversal
Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...
- Leetcode | Construct Binary Tree from Inorder and (Preorder or Postorder) Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal
原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/ 题 ...
- [LeetCode] 889. Construct Binary Tree from Preorder and Postorder Traversal 由先序和后序遍历建立二叉树
Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...
- LC 889. Construct Binary Tree from Preorder and Postorder Traversal
Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...
- 【LeetCode】889. Construct Binary Tree from Preorder and Postorder Traversal 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- (二叉树 递归) leetcode 889. Construct Binary Tree from Preorder and Postorder Traversal
Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...
- codeforce 1311E. Construct the Binary Tree (构造,就是个模拟)
ACM思维题训练集合 You are given two integers n and d. You need to construct a rooted binary tree consisting ...
- What is the difference between a binary tree, a binary search tree, a B tree and a B+ tree?
Binary Tree : It is a tree data structure in which each node has at most two children. As such there ...
随机推荐
- SQL的四种连接
SQL的四种连接-内连接.左外连接.右外连接.全连接 今天在看一个遗留系统的数据表的时候发现平时查找的视图是FULL OUT JOIN的,导致平时的数据记录要进行一些限制性处理,其实也可以设置视图 ...
- shell 判断问题总结
#!/bin/bash #比如需要判断一个变量是否含有值: if [[ -z $1 ]] ; thenecho "Something like empty!"exit 0;fi # ...
- Mongodb基本操作入门,增删改查和索引
主要进程 mongod.exe为启动数据库实例的进程. mongo是一个与mongod进程进行交互的JavaScript shell进程,它提供了一些交互的接口函数用户对数据库的管理. 基本命令 sh ...
- jsp页面用java代码取随机数
<%int seconds = (int) (System.currentTimeMillis() / 1000); %> 然后在需要随机数的地方加上下面的代码: <input na ...
- android学习笔记(3)Button控件的学习
一,增加一个button并用外部类绑定事件 //XML文件: <Button android:id="@+id/button1" android:layout_width=& ...
- Unity3D占用内存太大怎么解决呢?
最近网友通过网站搜索Unity3D在手机及其他平台下占用内存太大. 这里写下关于Unity3D对于内存的管理与优化. Unity3D 里有两种动态加载机制:一个是Resources.Load,另外一个 ...
- ThinkPHP3.1URL分发BUG修正
请先留意以下PHP脚本 PHP脚本A(http://127.0.0.1:8110/test.php): $url = 'http://127.0.0.1:8110/demo.php'; //curl请 ...
- R中导入excel乱码的解决办法
本文操作系统环境为win10,使用Rstdio. 要说明windows下在使用Rstdio的时候,在使用xlsx包,导入excel表乱码的解决办法. 1.我们先安装xlsx包 install.pack ...
- shiro设置session超时
通过api:Shiro的Session接口有一个setTimeout()方法 //登录后,可以用如下方式取得session SecurityUtils.getSubject().getSession( ...
- 什么是 Service Mesh
作者|敖小剑 微服务方兴未艾如火如荼之际,在 spring cloud 等经典框架之外,Service Mesh 技术正在悄然兴起.到底什么是 Service Mesh,它的出现能带来什么,又能改变什 ...