【原创】leetCodeOj ---Construct Binary Tree from Preorder and Inorder Traversal 解题报告
原题地址:
https://oj.leetcode.com/problems/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.
方法:
非常单纯的递归应用。当然首先你需要知道什么叫前序遍历,什么叫中序遍历。我们可以通过前序和中序遍历来构造一棵树,这个过程是递归的。
首先,我们拿到了两个序列。
其次,我们根据前序遍历序列,获得了树根,new一个树根。
然后,我们找到树根在中序遍历的位置,然后就知道左右子树的范围了。
最后,递归处理左右子树
PS:利用后序遍历和中序遍历来构造一棵树也是一样道理,关键有两点,一是找根,而是确定子树范围。
全部代码:
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
if (preorder.length == 0)
return null;
return trueStuff(preorder,0,preorder.length - 1,inorder,0,inorder.length - 1);
}
private TreeNode trueStuff(int[] pre,int startp,int finp,int[] in,int starti,int fini)
{
if (startp == finp)
return new TreeNode(pre[startp]);
if (startp > finp)
return null;
TreeNode tmp = new TreeNode(pre[startp]);
int pos = findIndex(in,starti,fini,pre[startp]);
if (pos == -1)
return null;
tmp.left = trueStuff(pre,startp + 1,startp + pos - starti,in,starti,pos - 1);
tmp.right = trueStuff(pre,startp + pos - starti + 1,finp,in,pos + 1,fini);
return tmp;
}
private int findIndex(int[] in,int start,int fin,int target)
{
for (int i = start;i <= fin;i++)
if (in[i] == target)
return i;
return -1;
}
}
【原创】leetCodeOj ---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 ...
- C++版-剑指offer 面试题6:重建二叉树(Leetcode105. Construct Binary Tree from Preorder and Inorder Traversal) 解题报告
剑指offer 重建二叉树 提交网址: http://www.nowcoder.com/practice/8a19cbe657394eeaac2f6ea9b0f6fcf6?tpId=13&tq ...
- 【LeetCode】889. Construct Binary Tree from Preorder and Postorder Traversal 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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 ...
- 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 ...
- 【题解二连发】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 ...
- 【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 ...
- [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 ...
随机推荐
- win7下怎样设置putty免用户名密码登陆
putty是一款好用的远程登录linux服务器软件,但每次输入用户名密码毕竟有些烦人,这里教你免用户名密码登陆. 工具/原料 putty 方法/步骤 去百度下载putty,小巧易用,仅有0.5 ...
- [Android阅读代码]android-async-http源码学习一
android-async-http 下载地址 一个比较常用的Http请求库,基于org.apache.http对http操作进行封装. 特点: 1.每一个HTTP请求发生在UI线程之外,Client ...
- A Game of Thrones(20) - Eddard
Eddard Stark rode through the towering bronze doors of the Red Keep sore, tired, hungry, and irritab ...
- Python语言总结 4.2. 和字符串(str,unicode等)处理有关的函数
4.2.7. 去除控制字符:removeCtlChr Python语言总结4.2. 和字符串(str,unicode等)处理有关的函数Sidebar Prev | Up | Next4.2.7 ...
- ORACLE RMAN介绍
本地连接: $ rman target / or $ rman target / nocatalog 远程连接: $ rman target sys/sys@sky RMAN命令执行方式: 1.单条 ...
- ZOJ3805:Machine
In a typical assembly line, machines are connected one by one. The first machine's output product wi ...
- Exception in thread "main" java.lang.IllegalArgumentException
1.错误叙述性说明 Exception in thread "main" java.lang.IllegalArgumentException: Cannot format giv ...
- hdu3329(2次dfs)
传送门:The Flood 题意:当水的高度升为多少的时候,能够将这块区域分为两个部分. 分析:枚举高度,先从外围开始一次dfs,将水能淹没的标记,然后看非标记的是否已分为多块. #include&l ...
- hdu4004(二分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4004 大致题意 二分最大跳跃能力,判断是否可以在m次内到达对岸! 分析:由于求青蛙最小弹跳能力,所以二 ...
- WASP_百度百科
WASP_百度百科 WASP