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 = [9,3,15,20,7]
postorder = [9,15,7,20,3]

Return the following binary tree:

    3
/ \
9 20
/ \
15 7 -------------------------------------------------------------------------------------
就是从中序遍历和后序遍历构建二叉树。可以用递归方式。注意递归的终止条件。
leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal几乎一样。 参考博客:http://www.cnblogs.com/grandyang/p/4296193.html C++代码:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
return build(inorder,,inorder.size()-,postorder,,postorder.size() - );
}
TreeNode *build(vector<int> &inorder,int ileft,int iright,vector<int> &postorder,int pleft,int pright){
if(ileft > iright ||pleft > pright) return NULL; //终止条件,就是当序列的长度为0时,递归终止。
int i = ;
TreeNode *cur = new TreeNode(postorder[pright]);
for(i = ileft; i < inorder.size(); i++){
if(inorder[i] == cur->val)
break;
}
cur->left = build(inorder,ileft,i-,postorder,pleft,pleft + i - ileft - );
cur->right = build(inorder,i + ,iright,postorder,pleft + i - ileft,pright - );
return cur;
}
};

还有一个方法,就是建立几个数组,保存分割后的数组。不过时间会很长。

C++代码:

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
return build(inorder,postorder);
}
TreeNode *build(vector<int> &inorder,vector<int> &postorder){
if(inorder.size() == || postorder.size() == ) //递归条件,当然也可以加上if(inorder.size() == 1 || postorder.size() == 1)return cur;这个递归条件。
return NULL;
int rootval = postorder.back();
TreeNode *cur = new TreeNode(rootval);
int i = ;
for(i = ; i < inorder.size(); i++){
if(inorder[i] == rootval) break;
}
vector<int> inleft,inright;
vector<int> poleft,poright;
for(int j = ; j < i; j++){
inleft.push_back(inorder[j]);
poleft.push_back(postorder[j]);
}
for(int j = i + ; j < inorder.size(); j++){
inright.push_back(inorder[j]);
}
for(int j = i; j < postorder.size() - ; j++){
poright.push_back(postorder[j]);
}
cur->left = build(inleft,poleft);
cur->right = build(inright,poright);
return cur;
}
};

这两个方法从算法上看是一样的,只是代码的实现不同而已。

(二叉树 递归) leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal的更多相关文章

  1. 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: ...

  2. [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 ...

  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 由中序和后序遍历建立二叉树 C++

    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 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 ...

  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(medium)

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

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

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

随机推荐

  1. nginx 防止盗链

    1.测试盗链(www.html2.com 盗取 www.html5.com的图片) 2.防止盗链 符合盗链 —— 重写 说明:if ($invalid_referer) {,if的后面是有空格的,如果 ...

  2. Centos7VMware虚拟机最小化安装后,安装Tenda U12 USB无线网卡驱动

    前几天买下了Tenda U12 USB 无线网卡 ,想连接上无线玩玩,可惜买下折腾了一周才解决他它驱动问题,前后在VMware上装了十多次,测试了好几个内核版本才搞定,好了废话不多说,分享下我安装过程 ...

  3. Web开发人员学习路线图

    http://www.runoob.com/w3cnote/2018-web-developer.html

  4. 【PAT】A1002 A+B for Polynomials

    仅有两个要注意的点: 如果系数为0,则不输出,所以输入结束以后要先遍历确定系数不为零的项的个数 题目最后一句,精确到小数点后一位,如果这里忽略了,会导致样例1,3,4,5都不能通过

  5. Python基础——2函数

    函数 函数定义 def a(参数): 函数的参数:必选参数.默认参数.可变参数.命名关键字参数和关键字参数 a(x,y,z=11,*l,**b): a(x,y,z=11,*liat,city,job, ...

  6. 【Linux基础】查看硬件信息-硬盘

     一.基础知识 1.磁盘分区 磁盘的分区主要分为基本分区(primary partion)和扩充分区(extension partion)两种,基本分区和扩充分区的数目之和不能大于四个.且基本分区可以 ...

  7. 新的编辑工具IDE

    因为最近一段时间前端都是用EXTJS开发,之前用vs2012来编辑extjs,感觉缺少智能感应,很不方便.后来发现一款工具Visual Studio Code,非常棒. VS Code是免费,轻量,跨 ...

  8. LeetCode练习4 找出这两个有序数组的中位数

    给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1 和 nums2  ...

  9. (十五)The Search API

    Now let’s start with some simple searches. There are two basic ways to run searches: one is by sendi ...

  10. 基于element ui的级联选择器组件实现的分类后台接口

    今天在做资产管理系统的时候遇到一个分类的级联选择器,前端是用的element的组件,需要后台提供接口支持.     这个组件需要传入的数据结构大概是这样的,详细的可参考官方案例: [{ value: ...