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 ...
随机推荐
- linux下如何查看多核负载情况【转】
最近服务器负载超荷,CPU的单个荷使用率到了百分之八九十,内存占用超大,让给检测性能,不给源码,只给一个+ -g编译生成的执行文件,Fuck!!! 1, 在linux下载了valgrind,使用val ...
- LVS基本原理
LVS基本原理 简介 负载调度器.真实服务器群节点一起被称为LVS.LVS负载调度器(有时也称为负载平衡器),接收所服务的所有接入服务集群的请求,并决定集群中的哪个节点应该回复其请求. 1)负载调度器 ...
- Spring Boot中MyBatis的使用
orm框架的本质是简化编程中操作数据库的编码,发展到现在基本上就剩两家了,一个是宣称可以不用写一句SQL的hibernate,一个是可以灵活调试动态sql的mybatis,两者各有特点,在企业级系统开 ...
- js验证身份证类
var idCardNoUtil = { provinceAndCitys: {11:"北京",12:"天津",13:"河北",14:&qu ...
- 【ASP.NET】@Model类型的使用详解
有时需要在ASP.NET MVC4的视图的@model中使用多个类型的实例,.NET Framework 4.0版本引入的System.Tuple类可以轻松满足这个需求. 假设Person和Produ ...
- 【Spring】基于SpringMVC的图片验证码功能实现
后台实现代码: ImgController.java 文件 package cn.shop.controller; import java.awt.Color; import java.awt.Fon ...
- Postgresql 正则表达式
在postgresql中使用正则表达式时需要使用关键字“~”,以表示该关键字之前的内容需匹配之后的正则表达式,若匹配规则不需要区分大小写,可以使用组合关键字“~*”: 相反,若需要查询不匹配这则表达式 ...
- Android Studio找不到FragmentActivity类
右击项目——>open module settings——>选择第五个选项卡“Dependencies”——>点击加号——>选择第一个Library dependency——& ...
- 图床神器:七牛云 + Mpic + FScapture
概述 最近在搞Markdown的东西,遇到了一个很棘手的问题,即图片的显示:通用的图片,可以直接网上搜索,但有时候需要自己截一些图或者对下载的图片进行修改,在本地存储完全没有问题,但Markdown写 ...
- 【转】java原理—反射机制
一.什么是反射:反射的概念是由Smith在1982年首次提出的,主要是指程序可以访问.检测和修改它本身状态或行为的一种能力.这一概念的提出很快引发了计算机科学领域关于应用反射性的研究.它首先被程序语言 ...