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. Ubuntu Linux 环境变量PATH设置

    前俩天编译linux下rar解压软件的时候说找不到  /usr/local/linux 路径,在设定path变量的时候,忽然就想看看,path最原始的变量值是多少....(在环境变量文件中保存.... ...

  2. 成都达内推荐PHP书籍【update 2017.1.10】

    [初阶](基础知识及入门) 01. <PHP与MySQL程序设计(第4版)> 02. <深入浅出MySQL 数据库开发 优化与管理维护 第2版> 03. <实战Nginx ...

  3. PHP通过soap调用c#的WebService

    1:C# Contact public class Contact { private int _Id; private string _Name; public int id { get { ret ...

  4. 解决Winform程序在不同分辨率系统下界面混乱

    问题分析:          产生界面混乱的主要原因是,winform程序的坐标是基于点(Point)的,而Point又与DPI相关,具体就是 一英寸 =72Points 一英寸 =  96pixel ...

  5. iOS11新特性之LargeTitle

    UI风格 在iOS 11中,系统APP使用了这种UI风格.这种风格最明显的变化就是使用了iOS 11的新特性--Large Title和新的SearchController. Demo GitHub: ...

  6. Mac Apache Maven 配置

    1.配置准备工作 1)配置 Maven 准备工作 下载相关软件 apache-maven-3.5.3.zip Maven 官网 Maven for Mac 配置软件下载地址,密码:q9u3. Mave ...

  7. SourceTree 代码库管理工具

    1.SourceTree 简介 SourceTree 是拥有可视化界面的项目版本控制软件,适用于 git 项目管理,window.mac 均可用. 官网下载地址 SourceTree 其它下载地址 S ...

  8. 一个web.Config或app.Config自定义段configSections的示例

    一个web.Config或app.Config自定义段configSections的示例 越来越觉得,直接用配置文件app.Config或web.Config配置应用系统的运行参数,比自己做一个xml ...

  9. Easyui combobox 始终选择第一个的问题

    //必须指定 id 和 text $('#contact_city').combobox({ valueField:'id', textField:'text', });

  10. app-framework学习--官网地址及demo下载地址

    一起学习共同进步,加油..! 官网地址:http://app-framework-software.intel.com/ 下载地址:http://download.csdn.net/detail/ha ...