[LeetCode-21]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.
_______7______
/ \
__10__ ___2
/ \ /
4 3 _8
\ /
1 11
preorder = {7,10,4,3,1,2,8,11}
inorder = {4,10,3,1,7,11,8,2}
The first node in preorder alwasy the root of the tree. We can break the tree like:
1st round:
preorder: {7}, {10,4,3,1}, {2,8,11}
inorder: {4,10,3,1}, {7}, {11, 8,2}
_______7______
/ \
{4,10,3,1} {11,8,2}
part agin based on the preorder.
2nd round
left part right part
preorder: {10}, {4}, {3,1} {2}, {8,11}
inorder: {4}, {10}, {3,1} {11,8}, {2}
_______7______
/ \
__10__ ___2
/ \ /
4 {3,1} {11,8}
Same way to split {3,1} and {11,8}, yo will get the complete tree now.
_______7______
/ \
__10__ ___2
/ \ /
4 3 _8
\ /
1 11
c++
TreeNode *BuildTreePI(
vector<int> &preorder,
vector<int> &inorder,
int p_s, int p_e,
int i_s, int i_e){
if(p_s > p_e) return NULL;
int pivot = preorder[p_s];
int i = i_s;
for(;i<i_e;i++){
if(inorder[i] == pivot)
break;
}
int length1 = i-i_s-1;
int length2 = i_e-i-1;
TreeNode* node = new TreeNode(pivot);
node->left = BuildTreePI(preorder,inorder,p_s+1,length1+p_s+1,i_s, i-1);
node->right = BuildTreePI(preorder, inorder, p_e-length2, p_e, i+1, i_e);
return node;
}
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
return BuildTreePI(preorder,inorder,0,preorder.size()-1,0,inorder.size()-1);
}
java
public TreeNode buildTree(int[] preorder, int[] inorder) {
return buildPI(preorder, inorder, 0, preorder.length-1, 0, inorder.length-1);
}
public TreeNode buildPI(int[] preorder, int[] inorder, int p_s, int p_e, int i_s, int i_e){
if(p_s>p_e)
return null;
int pivot = preorder[p_s];
int i = i_s;
for(;i<i_e;i++){
if(inorder[i]==pivot)
break;
}
TreeNode node = new TreeNode(pivot);
int lenLeft = i-i_s;
node.left = buildPI(preorder, inorder, p_s+1, p_s+lenLeft, i_s, i-1);
node.right = buildPI(preorder, inorder, p_s+lenLeft+1, p_e, i+1, i_e);
return node;
}
[LeetCode-21]Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章
- [LeetCode] 105. 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 105. 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 105. 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 105 Construct Binary Tree from Preorder and Inorder Traversal ----- java
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++
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 t ...
- Java for LeetCode 105 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] 105. Construct Binary Tree from Preorder and Inorder Traversal (Medium)
原题 题意: 根据先序和中序得到二叉树(假设无重复数字) 思路: 先手写一次转换过程,得到思路. 即从先序中遍历每个元素,(创建一个全局索引,指向当前遍历到的元素)在中序中找到该元素作为当前的root ...
- Leetcode#105 Construct Binary Tree from Preorder and Inorder Traversal
原题地址 基本二叉树操作. O[ ][ ] [ ]O[ ] 代码: TreeNode *restore(vector< ...
随机推荐
- redis主从和主从切换
redis数据量增加,导致内存不够用,要迁移分离redis和程序: 1. 在新redis服务器上,启动一个redis实例,配置和master配置一致,不同的是配置文件中修改并启用 slave-read ...
- jQuery中事件绑定
一.前言 最近在做项目中要用到jQuery来绑定事件,首先想到的是$(selector).事件名();这样绑定事件的方式,这种方式对事件进行绑定其实也就是bind()方法,但当选择器匹配的元素过多,$ ...
- JAVA 利用SimpleDateFormat将String转换为格式化的日期
1. /** * 使用用户格式提取字符串日期 * * @param strDate 日期字符串 * @param pattern 日期格式 * @return */ public static Dat ...
- 383. Ransom Note【easy】
383. Ransom Note[easy] Given an arbitrary ransom note string and another string containing letters f ...
- 谱聚类python实践
聚类后: # -*- coding: utf-8 -*-"""Created on 09 05 2017 @author: similarface"" ...
- 【问题记录】MySQL中时间戳转日期格式和Java中时间戳转日期格式偶尔不一致
背景: MySQL的某个字段存放着一些时间戳格式的时间. 问题描述: Java程序将MySQL中的时间戳字段取出来,在Java程序中转成yyyy-MM-dd HH:mm:ss格式的时候,偶尔会出现转化 ...
- C++获取二维数组的元素个数
C/C++获取二维数组的大小/长度/元素个数 ][]; ]) /
- latex math
scalar -> lower case lettervector -> lower case bold lettermatrix -> upper case bold letter
- js或jquery实现页面打印(局部打印)
首先定义css样式: 复制代码代码如下: @media print { .noprint { display: none;color:green } } 对于不想打印的内容只用在标签中加上 cla ...
- jenkins登录使用cas认证
jenkins是目前打包管理项目最牛的工具.http://192.168.35.50:4000/jenkins%20plugin/system/74-cas-plugin.html