【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal
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 duplicates do not exist in the tree.
根据定义,后序遍历postorder的最后一个元素为根。
由于元素不重复,通过根可以讲中序遍历inorder划分为左子树和右子树。
递归下去即可求解。
/**
* Definition for binary tree
* 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 Helper(inorder, , inorder.size()-, postorder, , postorder.size()-);
}
TreeNode* Helper(vector<int>& inorder, int begin1, int end1, vector<int>& postorder, int begin2, int end2)
{
if(begin1 > end1)
return NULL;
else if(begin1 == end1)
return new TreeNode(inorder[begin1]); TreeNode* root = new TreeNode(postorder[end2]);
int i = begin1;
for(; i <= end1; i ++)
{
if(inorder[i] == postorder[end2])
break;
}
int leftlen = i-begin1; root->left = Helper(inorder, begin1, begin1+leftlen-, postorder, begin2, begin2+leftlen-);
root->right = Helper(inorder, begin1+leftlen+, end1, postorder, begin2+leftlen, end2-);
return root;
}
};

【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal的更多相关文章
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告
[LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...
- 【一天一道LeetCode】#106. Construct Binary Tree from Inorder and Postorder Traversall
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...
- LeetCode OJ 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 ...
- 【LeetCode】889. Construct Binary Tree from Preorder and Postorder Traversal 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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: ...
- 【LeetCode】105 & 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 ...
- 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 ...
- (二叉树 递归) 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 ...
- [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 ...
随机推荐
- AtCoder Beginner Contest 022 A.Best Body 水题
Best Body Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://abc022.contest.atcoder.jp/tasks/abc02 ...
- django 获取 POST 请求值的几种方法(转)
转载请注明出处:http://hi.baidu.com/leejun_2005/blog/item/9a37a22238f35c5bac34de54.html from:http://stackove ...
- oc/object-c/ios用int还是NSInteger
当需要使用int类型的变量的时候,可以像写C的程序一样,用int,也可以用NSInteger,但更推荐使用NSInteger,因为这样就不用考虑设备是32位的还是64位的. 可以看如下定义:#if _ ...
- TEA加密算法java版
这个算法简单,而且效率高,每次可以操作8个字节的数据,加密解密的KEY为16字节,即包含4个int数据的int型数组,加密轮数应为8的倍数,一般比较常用的轮数为64,32,16,推荐用64轮. 源代码 ...
- Java之基于Eclipse搭建SSH框架(上)
http://blog.csdn.net/snowwitch/article/details/50925382 http://www.cnblogs.com/hww123/archive/2016/0 ...
- pm2-web
A web based monitor for PM2. Multiple hosts With the release of 0.11 pm2 no longer uses TCP sockets ...
- linuxShell检测内网中存活主机,windows下net view/nmblookup
#!/bin/bash # Simple SHELL script for Linux and UNIX system monitoring with # ping command # code by ...
- linux清空文件方法
1.以下方法,清除后,文件大小为0 2.以下方法,清除后,文件大小为1 多了一个结束字符
- Unity3D 浅谈被忽略的Quality [转]
开始分享之前,我先墨迹几句... 最近在工作上,在交流群中,都会遇到一些问题.比如: 为什么打包Android Apk以后,图片变模糊了? 为什么移动端的阴影和电脑端不一样? 我的电脑明明配置很好,为 ...
- 从AIDL开始谈Android进程间Binder通信机制
转自: http://tech.cnnetsec.com/585.html 本文首先概述了Android的进程间通信的Binder机制,然后结合一个AIDL的例子,对Binder机制进行了解析. 概述 ...