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

  1. inorder = [,,,,]
  2. postorder = [,,,,]

Return the following binary tree:

  1. / \
  2.  
  3. / \

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

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

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

  1. TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
  2. return buildTree(inorder,,inorder.size()-,postorder,,postorder.size()-);
  3. }
  4.  
  5. TreeNode* buildTree(vector<int>& inorder,int ileft,int iright,vector<int>& postorder,int pleft,int pright){
  6. if(ileft>iright||pleft>pright)
  7. return NULL;
  8. TreeNode* root=new TreeNode(postorder[pright]);
  9. int i=;
  10. for(i=ileft;i<=iright;i++){
  11. if(inorder[i]==postorder[pright])
  12. break;
  13. }
  14. root->left=buildTree(inorder,ileft,i-,postorder,pleft,pleft+i-ileft-);
  15. root->right=buildTree(inorder,i+,iright,postorder,pleft+i-ileft,pright-);
  16. return root;
  17. }

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#服务端Web Api

    本教程已经录制视频,欢迎大家观看我在CSDN学院录制的课程:http://edu.csdn.net/lecturer/944

  2. bat 传递参数

    调用bat时,传递参数有个小问题,记录一下. 1.问题描述: 传递参数时,接收的值不对.传递了“1,2,3”,接收时,只剩下1.后面的没有了. 解决: 原因是bat取参时,语法弄错了. Syntax ...

  3. tensorboard No graph definition files were found No scalar data was found 解决方法

    logdir后面的路径不要加引号!!!! tensorboard --logdir='D:\WorkSpace\eclipse\tf_tr\train\my_graph\' 删除引号,改为: tens ...

  4. Linux第十节课学习笔记

    部署LVM三步: 1.pv:使设备支持LVM: 2.vg:对支持LVM的设备进行整合: 3.lv:将整合的空间进行切割. 每个基本单元PE的大小为4M,分配空间必须是4M的整数倍.可以容量或基本单元个 ...

  5. 【python】问题汇总

    1.pip降级 python -m pip install pip==9.0.3 2. Flask利用pymysql出现Warning:1366的解决办法 错误提示:(1366, "Inco ...

  6. How to run Python code from Sublime

    How to run Python Code from Sublime,and How to run Python Code with input from sublime Using Sublime ...

  7. Python循环_for&while

    格式:for x in xs['James','Lily','Candy']: print(x) —————————————————————————————————— for循环就是把每个元素代入变量 ...

  8. DNS子域授权

    DNS子域授权 当一个域很大时,而且还有上,下层关系,如果所有的记录变更都由某一台服务器来管理的话,那将会是什么样子?就好比一个公司的总经理直接管理公司1000个人的所有事项,恐怕会被累死.所以会在总 ...

  9. BootStrap小知识随笔

    1.让label和input在一行显示 添加class "form-inline"就可以了.如 <table class="table table-bordered ...

  10. C 标准库头文件

    头文件 说明 头文件 说明 <assert.h> 条件编译宏,将参数与零比较 <complex.h> (C99 起) 复数运算 <ctype.h> 用来确定包含于字 ...