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 解题报告的更多相关文章

  1. 【原创】leetCodeOj ---Construct Binary Tree from Preorder and Inorder Traversal 解题报告

    原题地址: https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 题目 ...

  2. C++版-剑指offer 面试题6:重建二叉树(Leetcode105. Construct Binary Tree from Preorder and Inorder Traversal) 解题报告

    剑指offer 重建二叉树 提交网址: http://www.nowcoder.com/practice/8a19cbe657394eeaac2f6ea9b0f6fcf6?tpId=13&tq ...

  3. [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 ...

  4. 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 ...

  5. 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 ...

  6. [leetcode]Construct Binary Tree from Preorder and Inorder Traversal @ Python

    原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 题意:根 ...

  7. 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 ...

  8. 【LeetCode】889. Construct Binary Tree from Preorder and Postorder Traversal 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. 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 ...

随机推荐

  1. Solr删除managedschema

    一.创建solr核心 solr createcore 二.删除managedschema managedschema是solr自动生成的,里面包含大量无用配置. solr是检查用户定义的schema. ...

  2. 解决编译Apache出现的问题:configure: error: APR not found

    今日编译apache时出错: #./configure --prefix……检查编辑环境时出现: checking for APR... noconfigure: error: APR not fou ...

  3. mysql my.cnf 配置建议

    mysql的配置文件是/etc/my.cnf,通过修改它来配置mysql. /etc/my.cnf来自以下文件: 如果你的内存≤64M,则复制/usr/local/share/mysql/my-sma ...

  4. 树莓派进阶之路 (016) - 通过595驱动4位LED显示系统时间

    模块图片,4位共阳极数码管. 我们使用树莓派wiringPi的库来通过74HC595驱动4位数码管: C 代码如下: #include <wiringPi.h> #include < ...

  5. 信号之sigsetjmp和siglongjmp函数(转)

    在信号处理程序中经常调用longjmp函数以返回到程序的主循环中,而不是从该处理程序返回. 但是,调用longjmp有一个问题.当捕捉到一个信号时,进入信号捕捉函数,此时当前信号被自动地加到进程的信号 ...

  6. 数十种TensorFlow实现案例汇集:代码+笔记

    这是使用 TensorFlow 实现流行的机器学习算法的教程汇集.本汇集的目标是让读者可以轻松通过案例深入 TensorFlow. 这些案例适合那些想要清晰简明的 TensorFlow 实现案例的初学 ...

  7. UIAlertView 点击按钮后控制其是否消失

    新建NotDismissAlertView类,继承UIAlertView. 设置控制是否消失的标示符 重写  在-(void)dismissWithClickedButtonIndex:(NSInte ...

  8. java多线程学习--java.util.concurrent (转载)

    题记:util和concurrent 包是后续重点先看的和学习的模块 原文地址:http://www.cnblogs.com/sunhan/p/3817806.html CountDownLatch, ...

  9. mysql快速移植表数据

    使用select into outfile xxx ,  load data infile xxx 例如 : SELECT * into outfile '/tmp/out.txt' FROM `db ...

  10. Oracle 12C -- temporal validity

    temporal validity需要在创建表的时候使用一个vaild-time维度(包含时间起始和结束) 创建有valid-time维度的表 (1)显式指定两个date-time列 SQL> ...