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 ...
随机推荐
- Qt 静态编译后的exe太大, 能够这样压缩.
1. 下载PECompact 下载地址:http://download.csdn.net/download/sniper_bing/7669247 , 不行大家就去baidu搜索下载就能够了这个是绿 ...
- PowerDesigner之设置(3)——根据Name首字母生成Code
SQL版本:2000 PowerDesigner版本:16 网上有不少介绍 PowerDesigner Name/Code自动调整 的文章,但基本如出一辙. 这里,我就介绍下如何根据输入的Name根据 ...
- web容器 web服务器 servlet/jsp容器 之间的区别和关系是什么?
web容器 web服务器 servlet/jsp容器 之间的区别和关系是什么? 这是我在网上找的一些资料:1. Web浏览器除了可以在本地硬盘上打开网页文档外,还可以使用http网络协议从网络上的We ...
- 【BZOJ4974】字符串大师 KMP
[BZOJ4974]字符串大师 Description 一个串T是S的循环节,当且仅当存在正整数k,使得S是T^k(即T重复k次)的前缀,比如abcd是abcdabcdab的循环节.给定一个长度为n的 ...
- SharePoint服务器端对象模型 之 使用CAML进行数据查询(Part 2)
(三)使用SPQuery进行列表查询 1.概述 列表查询主要是指在一个指定的列表(或文档库)中按照某些筛选.排序条件进行查询.列表查询主要使用SPQuery对象,以及SPList的GetItems方法 ...
- Java 科学计数法
目录 Java 科学计数法 1 科学计数法的概念 1.1 有效数字 1.2 E记号 2 Java中的科学计数法 2.1 NumberFormat 2.2 DecimalFormat 2.3 BigDe ...
- <2013 06 29> In Deutschland. Thinking in Mechanism, EE, CS, etc.
一 在德国近一个月了,主要去了慕尼黑周边的几个景点,天鹅城堡啊,国王湖啊,然后就是在市区逛,玛丽安广场,伊萨河,英国公园,德意志博物馆... ... 总体的印象是非常好的,只是自己不怎么懂德语,但这里 ...
- ehcache 配置持久化到硬盘(四)
Ehcache默认配置的话 为了提高效率,所以有一部分缓存是在内存中,然后达到配置的内存对象总量,则才根据策略持久化到硬盘中,这里是有一个问题的,假如系统突然中断运行 那内存中的那些缓存,直接被释放掉 ...
- 安装VS2013 2015 需要IE10浏览器 跳过的方法
安装VS2013 如果浏览器版本较旧的话会提示要求你更新到IE10版本,很麻烦,那么我们如何跳过呢? 复制下面代码粘贴到文本文档里,修改文本txt后缀为bat,右键管理员运行. @ECHO OFF ...
- MySQL中自增长与锁
在InnoDB存储引擎中,对于每个含有自增长值的表都有一个自增长计数器(auto-increment counter)当对含有自增长的计数器的表进行插入操作时,这个计数器会被初始化,执行如下的语句可以 ...