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. linux systemd详解

    CentOS 7 使用systemd替换了SysV.Systemd目的是要取代Unix时代以来一直在使用的init系统,兼容SysV和LSB的启动脚本,而且够在进程启动过程中更有效地引导加载服务. s ...

  2. HP Elitebook 830 G5/Win10蓝屏 UcmUcsi.sys 错误解决

    转自https://support.hp.com/cn-zh/document/c06038185 注意BIOS可能是英文的,别着急,对着找就好了.

  3. 测试miniconda,python以及机器学习包是否安装成功

    1.测试安装版本 conda -V python -V 2.安装的命令 (1)库升级和安装 升级全部库:  conda upgrade --all [不知道为什么,我的conda install nu ...

  4. python------mysql API

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

  5. 详解Vue 非父子组件通信方法(非Vuex)

    假设 bb 组件里面有个按钮,点击按钮,把 123 传递给 aa 组件 // 根组件(this.$root) new Vue({ el: '#app', router, render: h => ...

  6. Linux 命令备忘

    1.查看所有正在运行的进程:ps -A 2.运行一个可执行文件(切换到目标目录下,LinuxProject3为可执行文件名):./LinuxProject3

  7. Java 静态代码的作用

    public student{ private static int MAXNUM=100; static{ System.out.println(MAXNUM); } student(){ Syst ...

  8. QCAD 修改默认的线宽

    QCAD 修改默认的线宽 默认的宽度实在是太宽了,把一些细节给掩盖了. 可以按以下方法找到修改默认宽度. 在 Layer -> Edit Layer 中. 最开始找了好久好久. 最开始在这里找了 ...

  9. 19.3 Table 1-2.S3C2440A 289-Pin FBGA Pin Assignments (Sheet 4 of 9) (Continued)

    应该为GPA22,这个在中文翻译手册里是正确的.

  10. [tomcat]tomcat 9.0.x 控制台中文乱码解决办法

    根本原因,tomcat 输出的东西,与cmd控制台或者IDE控制台编码不一致. 修改tomcat输出内容的编码,%CATALINA_HOME%/conf/logging.properties 9.0. ...