LeetCode——Construct Binary Tree from Inorder and Postorder Traversal
Question
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
Solution
参考:http://www.cnblogs.com/zhonghuasong/p/7096300.html
Code
/**
* 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) {
if (inorder.size() == 0 || postorder.size() == 0)
return NULL;
return ConstructTree(inorder, postorder, 0, inorder.size() - 1, 0, postorder.size() - 1);
}
TreeNode* ConstructTree(vector<int>& inorder, vector<int>& postorder,
int in_start, int in_end, int post_start, int post_end) {
int rootValue = postorder[post_end];
TreeNode* root = new TreeNode(rootValue);
if (in_start == in_end) {
if (post_start == post_end && inorder[in_start] == postorder[post_start])
return root;
}
int rootIn = in_start;
while (rootIn <= in_end && inorder[rootIn] != rootValue)
rootIn++;
int leftPostLength = rootIn - in_start;
if (leftPostLength > 0) {
// 注意起点和终点的写法
root->left = ConstructTree(inorder, postorder, in_start, rootIn - 1, post_start, post_start + leftPostLength - 1);
}
if (leftPostLength < in_end - in_start) {
root->right = ConstructTree(inorder, postorder, rootIn + 1, in_end, post_start + leftPostLength, post_end - 1);
}
return root;
}
};
LeetCode——Construct Binary Tree from Inorder and 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 ...
- 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 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 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 Week14]Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...
- 【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 Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- 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: ...
随机推荐
- mvc 二级域名 重定向
使用mvc开发了一个独立的站点(wechat),但是最后要和并到另外一个站点下(admin),但是外部访问要使用另一个站点(admin)的二级域名 考虑之后采用mvc路由机制来实现(这也要考虑),代码 ...
- 《从零开始学Swift》学习笔记(Day 26)——可选链
原创文章,欢迎转载.转载请注明:关东升的博客 在Swift程序表达式中会看到问号(?)和感叹号(!),它们代表什么含义呢?这些符号都与可选类型和可选链相关,下面来看看可选链. 可选链: 类图: 它们之 ...
- linux解压war包
可以用unzip命令 unzip project.war -d project 这样就在当前目录下解压project.war到project目录里面,参数-d的意思是创建project目录 附:unz ...
- minimum viable product
w https://en.wikipedia.org/wiki/Minimum_viable_product
- Architectural Styles and the Design of Network-based Software Architectures
w Architectural Styles and the Design of Network-based Software Architectures http://www.ics.uci.ed ...
- Webshell清除-解决驱动级文件隐藏挂马
Webshell清除-解决驱动级文件隐藏挂马
- 在Tomcat配置JNDI数据源的三种方式
最近使用到了在tomcat下配置数据源的内容,在这里转载一篇文章记录下 转载自: http://blog.csdn.net/dyllove98/article/details/7706218 在我过去 ...
- QThread与多线程(比较清楚)
QThread类为我们提供了一种平台无关的管理线程的方式.一个QThread对象管理应用程序中的一个线程,该线程从run()函数开始执行.并且,默认情况下,我们可以在run()函数中通过调用QThre ...
- standard pbr(三)-BRDF
// Default BRDF to use: #if !defined (UNITY_BRDF_PBS) // allow to explicitly override BRDF in custom ...
- SQL 排序的N种方法
一.手工查询得出名次 select * ,select count(*)+1 from T2 where T2.[成绩] > T1.[成绩] as 名次 from T1 结果: 1 2 3 3 ...