LeetCode——Construct Binary Tree from Preorder and Inorder Traversal
Question
Given preorder and inorder 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/7096150.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>& preorder, vector<int>& inorder) {
if (preorder.size() == 0 || inorder.size() == 0)
return NULL;
return ConstructTree(preorder, inorder, 0, preorder.size() - 1, 0, inorder.size() - 1);
}
TreeNode* ConstructTree(vector<int>& preorder, vector<int>& inorder,
int pre_start, int pre_end, int in_start, int in_end) {
int rootValue = preorder[pre_start];
TreeNode* root = new TreeNode(rootValue);
if (pre_start == pre_end) {
if (in_start == in_end)
return root;
}
int rootIn = in_start;
while (rootIn <= in_end && inorder[rootIn] != rootValue)
rootIn++;
int preLeftLength = rootIn - in_start;
if (preLeftLength > 0) {
root->left = ConstructTree(preorder, inorder, pre_start + 1, preLeftLength, in_start, rootIn - 1);
}
// 左子树的对大长度就是in_end - in_start
if (preLeftLength < in_end - in_start) {
root->right = ConstructTree(preorder, inorder, pre_start + 1 + preLeftLength, pre_end, rootIn + 1, in_end);
}
return root;
}
};
LeetCode——Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章
- LeetCode: Construct Binary Tree from Preorder and Inorder Traversal 解题报告
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- [LeetCode] 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 ...
- Leetcode 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 ...
- [leetcode]Construct Binary Tree from Preorder and Inorder Traversal @ Python
原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 题意:根 ...
- Leetcode: Construct Binary Tree from Preorder and Inorder Traversal, Construct Binary Tree from Inorder and Postorder Traversal
总结: 1. 第 36 行代码, 最好是按照 len 来遍历, 而不是下标 代码: 前序中序 #include <iostream> #include <vector> usi ...
- 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】105. Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- 36. Construct Binary Tree from Inorder and Postorder Traversal && Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/cons ...
- 【题解二连发】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 - LeetCode Construct Binary ...
随机推荐
- The server encountered an internal error that prevented it from fulfilling this request.(JsonMappingException: Conflicting getter definitions)
在测试一个方法,dubug查看查询结果已经出来了,结果页面上是The server encountered an internal error that prevented it from fulfi ...
- dict字典使用方法
keys(). values() .items()方法 1.返回格式 dict_keys. dict_values 和 dict_items 2.常用于循环.迭代 for key in dict_te ...
- 使用jenkins进行Android的持续集成
关于持续集成的定义和意义可以参考它的 百度百科 主要意义有以下几点: 减少风险 减少重复过程 任何时间.任何地点生成可部署的软件 增强项目的可见性 建立团队对开发产品的信心 持续集成的实施 持续集成的 ...
- Parenthesis(前缀和+线段树)
1809: Parenthesis Time Limit: 5 Sec Memory Limit: 128 Mb Submitted: 2291 Solved: 622 Des ...
- 相似度模型 similarity model
Lucene4.0附加了相似度模型,允许在文档中使用不同的公式.
- 安装和配置jenkins
1.首先准备java环境,安装JDK 2.下载jenkins至Linux服务器 sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkin ...
- Bootstrap学习-排版-表单
1.标题 <h1>~<h6>,所有标题的行高都是1.1(也就是font-size的1.1倍). 2.副标题 <small>,行高都是1,灰色(#999) <h ...
- MySql 安装常见问题汇总
说明: 以下是针对 Mac 10.11 系统 以前,安装 MySql 数据库后, 设置的密码过于复杂,想更改为简单的密码, 方便数据库的使用. 1. 关闭和启动 MySql 数据库的方法: Syste ...
- Mysql常用优化方案
摘自:http://www.jb51.net/article/18934.htm 1.选取最适用的字段属性 MySQL可以很好的支持大数据量的存取,但是一般说来,数据库中的表越小,在它上面执行的查询也 ...
- python 时间模块小结
python有两个重要的时间模块,分别是time和datetime time模块 表示时间的几种方法 时间元组 time.struct_time( tm_year=2016, tm_mon=7, tm ...