LeetCode: Construct Binary Tree from Preorder and Inorder Traversal 解题报告
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.
SOLUTION 1:
1. Find the root node from the preorder.(it is the first node.)
2. Try to find the position of the root in the inorder. Then we can get the number of nodes in the left tree.
3. 递归调用,构造左子树和右子树。
例子:
Pre: 4 2 1 3 6 5 7
Inorder: 1 2 3 4 5 6 7
/**
* 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) {
// bug 3: consider when length is 0.
if (preorder == null || inorder == null || preorder.length == 0 || preorder.length != inorder.length) {
return null;
} // bug 4: end index is length - 1.
return buildTree(preorder, inorder, 0, preorder.length - 1, 0, preorder.length - 1);
} public TreeNode buildTree(int[] preorder, int[] inorder, int preStart, int preEnd, int inStart, int inEnd) {
// base case;
if (preStart > preEnd) {
return null;
} int rootVal = preorder[preStart];
TreeNode root = new TreeNode(rootVal); int pos = findTarget(inorder, rootVal, inStart, inEnd); // bug 5: left number is pos - instart can't add 1
int leftNum = pos - inStart; root.left = buildTree(preorder, inorder, preStart + 1, preStart + leftNum, inStart, pos - 1);
root.right = buildTree(preorder, inorder, preStart + leftNum + 1, preEnd, pos + 1, inEnd); return root;
} // bug 1: return type required.
// bug 2: this is not a bst. can't use binary search.
public int findTarget(int[] A, int target, int start, int end) {
for (int i = start; i <= end; i++) {
if (target == A[i]) {
return i;
}
} return -1;
}
}
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/tree/BuildTree.java
LeetCode: Construct Binary Tree from Preorder and Inorder Traversal 解题报告的更多相关文章
- 【原创】leetCodeOj ---Construct Binary Tree from Preorder and Inorder Traversal 解题报告
原题地址: https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 题目 ...
- C++版-剑指offer 面试题6:重建二叉树(Leetcode105. Construct Binary Tree from Preorder and Inorder Traversal) 解题报告
剑指offer 重建二叉树 提交网址: http://www.nowcoder.com/practice/8a19cbe657394eeaac2f6ea9b0f6fcf6?tpId=13&tq ...
- [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
Question Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may as ...
- [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】889. Construct Binary Tree from Preorder and Postorder Traversal 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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 ...
随机推荐
- java struts2入门学习实例--使用struts2快速实现多个文件上传
一.错误提示信息配置 昨天说到更改默认错误配置信息,我测试很多遍,一直都不对.下面贴出来,待以后有好方法了再补充吧. 首先新建一个properties文件,这里命名为testupload.proper ...
- [Spring学习笔记 3 ] spring 注解详解,完全注解,常用注解
.xml使用注解 xml 用来定义bean的信息,注解用来配置依赖信息 ) 在配置文件中配置bean )在javaBean中用注解来指定依赖注入 )在配置文件中开启注解扫描 @Resource标签 j ...
- linux之间文件传输(之scp)
linux的scp命令 linux 的 scp 命令 可以 在 linux 之间复制 文件 和 目录: ==================scp 命令==================scp 可以 ...
- 解决servlet-api包冲突问题(maven)
问题描述:本人的项目是用Maven管理,而且用到了servlet3.0的技术,但是项目中用到servlet3.0的地方,总提示找不到类中的方法.很奇怪,在网上找到好多解决办法,综合一下终于解决了.现将 ...
- Linux下通过关键字模糊查找搜索文件
[背景] 想要在Linux下面,找之前不知道放到哪里的一个tomcat的文件. [折腾过程] 1.最后是参考: linux查找文件命令find – 发芽的石头 – 博客频道 – CSDN.NET 去搜 ...
- python练习笔记——面试题 F(n) = F(n-1)+F(n-2)
已知:F(0) = 0, F(1) = 1, F(n) = F(n-1) + F(n-2) 其中(n≥2,n∈N*) 求:求10以内的函数值分别是多少 方法一: def F(n): if n < ...
- 解决Clover在win 10下的兼容问题
周五闲的蛋疼,把系统升级到win10.周一早上过来,发现Clover 无法使用了,各种崩溃,查阅了官网,发现Clover确实只兼容到win8.网络上给出解决方案的确是用qttabbar,qttabba ...
- Unable to convert MySQL date/time value to System.DateTime
当使用.NET connector 连接MYSQL数据库,检索某些数据的时候,你可能会得到一个错误信息: "Unable to convert MySQL date/time value t ...
- Linux VFS数据结构
先说明一下,linux内核中各种数据结构也不停的在变,所以不同版本的内核各个数据结构的定义可能会差别很大,这一组关于linux 文件系统的文章中的代码都摘自linux-2.6.34.1. VFS依赖于 ...
- labview中小黑点,小红点
小黑点:在labview中每一个小黑点就代表了一次内存的分配,通过小黑点可以帮助我们分析数据变量的内存拷贝情况