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. 对于"单链表逆置和递归"的问题的理解.

    一. 相关知识要点: 学习或了解基础数据结构和C语言, 对基础链表知识或相关知识有概况性认识. 例如: 本题目结构为: #define elem_type int typedef struct _si ...

  2. 将 windows 目录结构 复制到 linux 上

    思路:生成目录结构文件,复制到linux上,然后建立每个文件即可 借助unix_utils( https://sourceforge.net/projects/unxutils/) 1. window ...

  3. .Net牛刀小试-1缓冲使用

    根据文件名缓冲文件: /// <summary> /// 根据文件名缓冲指定目录文件 /// </summary> public class FileCacheAdapter ...

  4. FileUpload上传

    单文件上传: ASPX: <div> <!-- 文件上传 --> <asp:FileUpload ID="FileUpload1" runat=&qu ...

  5. idea构建maven多项目web架构

    1.新建一个maven项目作为顶级module,可以使用模板quickstart模板,在生成pom.xml中添加一些子项目都会用到的依赖,apache-commons,guava等.因为是顶级modu ...

  6. bootloader介绍

    Bootloader是系统加电后运行的第一段软件代码,主要任务就是将内核映像从硬盘读到RAM中,然后跳转到内核的入口点去运行,也即开始启动操作系统. 简单的说,Bootloader就是在操作系统运行内 ...

  7. FIRMWARE BUG – THE BIOS HAS CORRUPTED HW-PMU RESOURCES

    If you’re trying to install CentOS 7 on HP server and you receive the error from the caption, don’t ...

  8. fatal: could not read Username for 'https://git.dev.tencent.com' 解决方法

    在使用webhook自动部署时测试出现此问题,通过以下方法粗暴解决: vim .git/config 文件,在remote "origin"  url中加入帐号密码,如图所示,格式 ...

  9. 未来-IOT-Aliyun:ICA 联盟

    ylbtech-未来-IOT-Aliyun:ICA 联盟 ICA联盟介绍 为解决IoT标准体系不完善,场景复合或者分散,不同领域端云连接.通信组网.数据属性.端到端安全.测试标准缺乏或不统一等痛点问题 ...

  10. Exp3 免杀原理和实践

    一.基础问题回答 1.杀软是如何检测出恶意代码的? (1)基于特征码的检测 特征码是能识别一个程序是一个病毒的一段不大于64字节的特征串.如果一个可执行文件包含这样的特征码则被杀毒软件检测为是恶意代码 ...