【leetcode】Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
题解:如下图所示的一棵树:
5
/ \
2 4
/ \ \
1 3 6
中序遍历序列:1 2 3 5 4 6
后序遍历序列:1 3 2 6 4 5
后序遍历序列的最后一个元素就是当前根节点元素。首先想到的方法是递归,重要的是在先序和中序序列中分清楚哪一部分是左子树的,哪一部分是右子树的。
递归的过程如下图所示:其中in和po分别代表递归调用时传递给左子树的中序遍历序列和后序遍历序列。

以第一次递归为例,说明左右子树的中序和后序遍历序列如何求:

如上图所示,现在中序序列中找到根节点的位置(5),就能够知道左子树(绿色圈)和右子树(橙色圈)的中序遍历序列了。然后根据中序遍历的长度等于后序遍历的长度,计算出左右子树的后序遍历序列,递归调用构造树函数即可。
代码如下:
/**
* 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[] inorder,int[] postorder,int instart,int inend,int postart,int poend){
if(instart > inend)
return null;
TreeNode root = new TreeNode(postorder[poend]);
int index = InorderIndex(inorder, root.val);
root.left = buildTreeRec(inorder, postorder, instart, index-1, postart, postart+index-instart-1);
root.right = buildTreeRec(inorder, postorder, index+1, inend, postart+index-instart, poend-1); return root;
}
public TreeNode buildTree(int[] inorder, int[] postorder) {
return buildTreeRec(inorder, postorder, 0, inorder.length-1, 0, postorder.length-1);
}
}
【leetcode】Construct Binary Tree from Inorder and Postorder Traversal的更多相关文章
- 【Leetcode】【Medium】Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- 【树】Construct Binary Tree from Inorder and Postorder Traversal
题目: Given inorder and postorder traversal of a tree, construct the binary tree. 思路: 后序序列的最后一个元素就是树根, ...
- 【LeetCode106】Construct Binary Tree from Inorder and Postorder Traversal★★
1.题目 2.思路 思路和LeetCode105类似,见上篇. 3.java代码 //测试 public class BuildTreeUsingInorderAndPostorder { publi ...
- 【题解二连发】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 ...
- [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...
- Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 31041 Total Submissions: ...
- leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree f
1. Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers ...
- (二叉树 递归) leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- [LeetCode] 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
随机推荐
- Spring Cloud 微服务四:熔断器Spring cloud hystrix
前言:在微服务架构中,一般都是进程间通信,有可能调用链都比较长,当有底层某服务出现问题时,比如宕机,会导致调用方的服务失败,这样就会发生一连串的反映,造成系统资源被阻塞,最终可能造成雪崩.在sprin ...
- DELL inspiron1420 linux下的wifi驱动安装
首先确定无线网卡类型: lspci -vnn -d 14e4: 比如我的网卡类型为 04:00.0 Network controller [0280]:Broadcom Corporation BCM ...
- 【BZOJ3331】[BeiJing2013]压力 Tarjan求点双
[BZOJ3331][BeiJing2013]压力 Description 如今,路由器和交换机构建起了互联网的骨架.处在互联网的骨干位置的核心路由器典型的要处理100Gbit/s的网络流量.他们每天 ...
- 重载(overload)、覆盖(override)和隐藏(hide)
写正题之前,先给出几个关键字的中英文对照,重载(overload),覆盖(override),隐藏(hide).在早期的C++书籍中,可能翻译的人不熟悉专业用语(也不能怪他们,他们不是搞计算机编程的, ...
- 洛谷 1641 [SCOI2010]生成字符串
题目戳这里 一句话题意 求\(C_{m+n}^{m}\)-\(C_{m+n}^{m-1}\) Solution 巨说这个题目很水 标签居然还有字符串? 但是我还不很会用逆元真的太菜了,还好此题模数P为 ...
- 程序运行之ELF文件结构
ELF目标文件格式的最前部是ELF文件头.包含了整个文件的基本属性.比如ELF文件版本,目标机器型号,程序入口地址等.然后是ELF的各个段,其中ELF文件中与段有关的重要结构就是段表.段表描述了ELF ...
- 使用svn diff的-r参数的来比较任意两个版本的差异
1 svn diff的用法1.1 对比当前本地的工作拷贝文件(working copy)和缓存在.svn下的版本库文件的区别 svn diff 1.2 对比当前本地的工作拷贝文件(working co ...
- rails跨域请求配置
gem 'rack-cors', '~> 0.3.1'application.rb config.middleware.insert_before 0, "Rack::Cors&quo ...
- centos 6.5 编译安装了 Nginx1.6.0+MySQL5.6.19+PHP5.5.14
centos 6.5 编译安装了 Nginx1.6.0+MySQL5.6.19+PHP5.5.14--------------------------------------------------- ...
- 模仿jquery框架源码 -成熟---选择器
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...