Given inorder and postorder traversal of a tree, construct the binary tree.

Note:
You may assume that duplicates do not exist in the tree.

For example, given

inorder = [,,,,]
postorder = [,,,,]

Return the following binary tree:

   / \

    /  \
      

中序、后序遍历得到二叉树,可以知道每一次新数组的最后一个数为当时子树的根节点,每次根据中序遍历的根节点的左右两边确定左右子树,再对应后序的左右子树,不停递归得到根节点,可以建立二叉树。每次由循环得到根节点在中序数组中坐标i

由中序遍历知:每一次inorder的左子树范围[ileft,i-1],右子树范围[i+1,iright]

由后序遍历知:每一次postorder的左子树范围[pleft,pleft+i-ileft-1],右子树范围[pleft+i-ileft,pright-1]。C++

 TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
return buildTree(inorder,,inorder.size()-,postorder,,postorder.size()-);
} TreeNode* buildTree(vector<int>& inorder,int ileft,int iright,vector<int>& postorder,int pleft,int pright){
if(ileft>iright||pleft>pright)
return NULL;
TreeNode* root=new TreeNode(postorder[pright]);
int i=;
for(i=ileft;i<=iright;i++){
if(inorder[i]==postorder[pright])
break;
}
root->left=buildTree(inorder,ileft,i-,postorder,pleft,pleft+i-ileft-);
root->right=buildTree(inorder,i+,iright,postorder,pleft+i-ileft,pright-);
return root;
}

LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树 C++的更多相关文章

  1. [LeetCode] 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  2. Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 31041 Total Submissions: ...

  3. (二叉树 递归) leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  4. LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal (用中序和后序树遍历来建立二叉树)

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  5. C#解leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  6. [leetcode] 106. Construct Binary Tree from Inorder and Postorder Traversal(medium)

    原题地址 思路: 和leetcode105题差不多,这道题是给中序和后序,求出二叉树. 解法一: 思路和105题差不多,只是pos是从后往前遍历,生成树顺序也是先右后左. class Solution ...

  7. Leetcode#106 Construct Binary Tree from Inorder and Postorder Traversal

    原题地址 二叉树基本操作 [       ]O[              ] [       ][              ]O 代码: TreeNode *restore(vector<i ...

  8. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    [LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...

  9. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

随机推荐

  1. SSH(Spring+Struts2+Hibernate) of mappings(SSH三大框架的映射问题)

    错误提示: org.springframework.orm.hibernate3.HibernateSystemException: Unknown entity org.hibernate.Mapp ...

  2. Spring mvc后台重定向页面,实际前端不跳转

    1.ajax不支持重定向 ajax是不支持重定向的,因为ajax本身就是局部刷新,不重新加载页面的. 2.若后台出现需要重定向页面,可以设置唯一错误码 前端ajax公共调用后,凡是遇到这一类错误码,则 ...

  3. ubuntu typora使用学习

    typora使用方法 标题: 对于标题,直接用ctrl+对应数字就是第几级标题 文字格式: ctrl+B/I/U 进入加粗/倾斜/下划模式,不需要符号键入 居中的话 用 CENTER 列表引用: 可直 ...

  4. python------mysql API

    参考引用博客:http://www.cnblogs.com/wupeiqi/articles/5713330.html ifconfig是linux中用于显示或配置网络设备(网络接口卡)的命令,英文全 ...

  5. TemplatePart特性的作用

    看wp控件的源代码时发现TemplatePart特性,于是在百度上查了查: http://blog.csdn.net/wushang923/article/details/9224533 Templa ...

  6. bond模式

    1.mode=0(balance-rr)(平衡抡循环策略) 链路负载均衡,增加带宽,支持容错,一条链路故障会自动切换正常链路.交换机需要配置聚合口,思科叫port channel.特点:传输数据包顺序 ...

  7. windows攻击实验

    实验目的:使用Metaspoit攻击MS08-067,提交正确得到远程Shell过程的截图(不少于5张) 靶机:192.168.11.231 攻击机:192.168.11.131 实验步骤: 1.首先 ...

  8. python实现将字符串中以大写字母开头的单词前面添加“_”下划线

    在工作中写测试用例代码生成的时候,函数命令考虑采用参数文件的名称来命名,但是发现文件命名是驼峰的写写法,所以想按照字符串中的大写字母做分割,每个单词前面添加下划线,主要考虑采用正则的模式来匹配,替换然 ...

  9. 剑指offer 12.代码的完整性 数值的整数次方

    题目描述 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方.   本人渣渣代码: public double Power(double ba ...

  10. h5需要的浏览器插件

    google浏览器插件: web前段助手.vue-tools.草料二维码