【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 duplicates do not exist in the tree.
类似http://www.cnblogs.com/sunshineatnoon/p/3854935.html
只是子树的前序和中序遍历序列分别更新为:
//左子树:
left_prestart = prestart+1
left_preend = prestart+index-instart
//右子树
right_prestart = prestart+index-instart+1
right_preend = preend
代码如下:
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int InorderIndex(int[] inorder,int key){
if(inorder == null || inorder.length == 0)
return -1; for(int i = 0;i < inorder.length;i++)
if(inorder[i] == key)
return i; return -1;
}
public TreeNode buildTreeRec(int[] preoder,int[] inorder,int prestart,int preend,int instart,int inend){
if(instart > inend)
return null;
TreeNode root = new TreeNode(preoder[prestart]);
int index = InorderIndex(inorder, root.val);
root.left = buildTreeRec(preoder, inorder, prestart+1, prestart+index-instart, instart, index-1);
root.right = buildTreeRec(preoder, inorder, prestart+index-instart+1, preend, index+1, inend); return root;
}
public TreeNode buildTree(int[] preorder, int[] inorder) {
return buildTreeRec(preorder, inorder, 0, preorder.length-1, 0, inorder.length-1);
}
}
【leetcode刷题笔记】Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章
- LeetCode(105) Construct Binary Tree from Preorder and Inorder Traversal
题目 Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume t ...
- 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 ...
- LeetCode: Construct Binary Tree from Preorder and Inorder Traversal 解题报告
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- 【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 ...
- 【题解二连发】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 ...
- 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 ...
- 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 ...
- [LeetCode] 105. 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】105. Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
随机推荐
- Coursera machine learning 第二周 quiz 答案 Linear Regression with Multiple Variables
https://www.coursera.org/learn/machine-learning/exam/7pytE/linear-regression-with-multiple-variables ...
- spring mvc 伪静态处理
spring mvc 伪静态处理 @RequestMapping(value = JsonUrlCommand.webshare_get_opuss+"/u{u:[\\w\\W]+}p{p: ...
- C++ 错误积累
错误一 VS2012错误:不能在成员函数 的类外部重新声明该函数 解决:检查函数的大括号匹配
- SET IDENTITY_INSERT ON/OFF 权限
今天突然遇到了,找不到对象“XXXX”,因为它不存在或者没有您所需的权限,于是检查程序,突然发现程序中有一段代码是: SET IDENTITY_INSERT eticket ON //执行业务 ... ...
- Labview新建项目步骤
打开Labview软件,点击工具栏中文件选项卡,如图所示. 2 点击新建一个空白项目. 3 此时为未命名项目,按下Ctrl+S保存项目到自己指定的目录并完成命名. 4 如图示在我的电脑上点击右键,新建 ...
- bash批量去前缀
#!/bin/sh for aFile in *; do oldfile=`basename "$aFile"` newfile=${oldfile::} echo ${oldfi ...
- [转]How Hash Algorithms Work
来看看SHA-1到底是如何工作的 http://www.metamorphosite.com/one-way-hash-encryption-sha1-data-software
- android 自定义progressbar 样式
在res下创建drawable文件夹,新建文件drawable/progressbar_color.xml <layer-list xmlns:android="http://sche ...
- 【Android】开源项目汇总
Android开源项目第一篇——个性化控件(View)篇 包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.Progres ...
- anaconda + opencv3
直接运行 pip install opencv-python 或者 pip install opencv-contrib-python 参照如下网页 https://blog.csdn.net/sin ...