Leetcode | Construct Binary Tree from Inorder and (Preorder or Postorder) Traversal
Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
递归构建。
思路就是: preorder可以定位到根结点,inorder可以定位左右子树的取值范围。
1. 由preorder得到根结点;把preorder第一个点删掉;
2. 先建左子树;再建右子树;
通过一个区间来表示左右子树的取值范围。因为inorder左右子树的范围都是连续的。中间就是root。
class Solution {
public:
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
return recursive(preorder, inorder, , inorder.size() - );
}
TreeNode* recursive(vector<int> &preorder, vector<int> &inorder, int s, int e) {
if (s > e) return NULL;
if (preorder.empty()) return NULL;
TreeNode *root = new TreeNode(preorder.front());
preorder.erase(preorder.begin());
int i = s;
for (; i <= e && inorder[i] != root->val; ++i);
root->left = recursive(preorder, inorder, s, i - );
root->right = recursive(preorder, inorder, i + , e);
}
};
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.
和上面类似。有两点不同。
1. postorder,最后一个元素是根结点。
2. 先构建右子树,再构建左子树。
class Solution {
public:
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
return recursive(postorder, inorder, , inorder.size() - );
}
TreeNode* recursive(vector<int> &postorder, vector<int> &inorder, int s, int e) {
if (s > e) return NULL;
if (postorder.empty()) return NULL;
TreeNode *root = new TreeNode(postorder.back());
postorder.pop_back();
int i = s;
for (; i <= e && inorder[i] != root->val; ++i);
root->right = recursive(postorder, inorder, i + , e);
root->left = recursive(postorder, inorder, s, i - );
}
};
Leetcode | Construct Binary Tree from Inorder and (Preorder or Postorder) Traversal的更多相关文章
- LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal
LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...
- 105. Construct Binary Tree from Inorder and preorder Traversal
/* * 105. Construct Binary Tree from Inorder and preorder Traversal * 11.20 By Mingyang * 千万不要以为root ...
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- Leetcode, construct binary tree from inorder and post order traversal
Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs ab ...
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...
- [LeetCode] Construct Binary Tree from Inorder and Pretorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- Leetcode 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]Construct Binary Tree from Inorder and Postorder Traversal @ Python
原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题意: ...
- [Leetcode] Construct binary tree from inorder and postorder travesal 利用中序和后续遍历构造二叉树
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume th ...
随机推荐
- Tomcat 在 Linux 下的自动启动脚本
很多服务都需要设置为开机自启动.将下面代码复制到 /etc/rc.d/init.d/tomcat ,然后执行 chkconfig –add tomcat chkconfig tomcat on 就可以 ...
- Flask With
- logback mybatis 打印sql语句
logbac.xml 文件的基础配置参考的园友的 http://www.cnblogs.com/yuanermen/archive/2012/02/13/2349609.html 然后hibernat ...
- Install ADDS on Windows Server 2012 R2 with PowerShell
Install ADDS on Windows Server 2012 R2 with PowerShell Posted by ethernuno on 20/04/2014 In this tut ...
- 【Longest Consecutive Sequence】cpp
题目: Given an unsorted array of integers, find the length of the longest consecutive elements sequenc ...
- Android版本对应的API号
Android版本 API 级别 1.5 API 3 1.6 API 4 2.1 API 7 2.2 API 8 2.3.3 API 10 3.0 API 11 3.1 API 12 3.2 API ...
- [python][django学习篇][7]设计博客视图(1)
1上网的流程: 打开浏览器,输入网址(http://zmrenwu.com/) 浏览器根据输入网址,完成以下几件事:1识别服务器地址,2将用户的浏览意图打包成一个http请求,发送给服务器,等待服务器 ...
- php 不重新编译增加openssl扩展
安装openssl和开发包 yum install openssl openssl-devel 跳转到PHP源码下的openssl cd /usr/local/src/php-5.5.27/ext/o ...
- Win右键管理员权限的获取
Windows Registry Editor Version 5.00 ;取得文件修改权限 [HKEY_CLASSES_ROOT\*\shell\runas] @="管理员权限" ...
- BZOJ3561 DZY Loves Math VI 【莫比乌斯反演】
题目 给定正整数n,m.求 输入格式 一行两个整数n,m. 输出格式 一个整数,为答案模1000000007后的值. 输入样例 5 4 输出样例 424 提示 数据规模: 1<=n,m<= ...