106 Construct Binary Tree from Inorder and Postorder Traversal 从中序与后序遍历序列构造二叉树
给定一棵树的中序遍历与后序遍历,依据此构造二叉树。
注意:
你可以假设树中没有重复的元素。
例如,给出
中序遍历 = [9,3,15,20,7]
后序遍历 = [9,15,7,20,3]
返回如下的二叉树:
3
/ \
9 20
/ \
15 7
详见:https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/description/
Java实现:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode buildTree(int[] inorder, int[] postorder) {
if(inorder==null||inorder.length==0||postorder==null||postorder.length==0||inorder.length!=postorder.length){
return null;
}
return buildTree(inorder,0,inorder.length-1,postorder,0,postorder.length-1);
}
private TreeNode buildTree(int[] inorder,int startIn,int endIn,int[] postorder,int startPost,int endPost){
if(startIn>endIn||startPost>endPost){
return null;
}
TreeNode root=new TreeNode(postorder[endPost]);
for(int i=startIn;i<=endIn;++i){
if(inorder[i]==postorder[endPost]){
root.left=buildTree(inorder,startIn,i-1,postorder,startPost,startPost+i-startIn-1);
root.right=buildTree(inorder,i+1,endIn,postorder,startPost+i-startIn,endPost-1);
}
}
return root;
}
}
106 Construct Binary Tree from Inorder and Postorder Traversal 从中序与后序遍历序列构造二叉树的更多相关文章
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告
[LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- 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 OJ 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 ...
- 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 ...
- C#解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】105 & 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 ...
- 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 ...
随机推荐
- Gym - 100187E E - Two Labyrinths —— bfs
题目链接:http://codeforces.com/gym/100187/problem/E 题解:一开始做的时候是将两幅图合并,然后直接bfs看是否能到达终点.但这种做法的错的,因为走出来的路对于 ...
- mac配置apache
http://www.cnblogs.com/snandy/archive/2012/11/13/2765381.html 用自带的 sudo apachectl -v sudo apachectl ...
- python 模块和模块sys.argv
In [5]: import os In [6]: os.__file__ Out[6]: '/usr/local/lib/python2.7/os.pyc' In [7]: import rando ...
- TortoiseGit创建本地库并提交到远程服务器
前半部分参考网上的例子:http://www.showerlee.com/archives/1300,但会出现“Git did not exit cleanly (exit code 128)”错误 ...
- 级联MobileNet-V2实现CelebA人脸关键点检测(转)
https://blog.csdn.net/u011995719/article/details/79435615
- 关于layer.open() 弹框的使用
在用 layer.open() 追加渲染HTML的时候,完全看不到效果,皆因layui框架需要在最后用 form.render() 方法来渲染表单才可以看到效果,写法如下: <script> ...
- Run-Time Check Failure #3 - The variable 'p' is being used without being initialized.
Run-Time Check Failure #3 - The variable 'p' is being used without being initialized. 运行时检查失败 变量p没有初 ...
- springmvc源码分析系列-请求处理流程
接上一篇-springmvc源码分析开头片 上一节主要说了一下springmvc与struts2的作为MVC中的C(controller)控制层的一些区别及两者在作为控制层方面的一些优缺点.今天就结合 ...
- 【网络爬虫】【java】微博爬虫(四):数据处理——jsoup工具解析html、dom4j读写xml
之前提到过,对于简单的网页结构解析,可以直接通过观察法.手工写正则解析,可以做出来,比如网易微博.但是对于结构稍微复杂点的,比如新浪微博,如果还用正则,用眼睛一个个去找,未免太麻烦了. 本文介绍两个工 ...
- UVaLive 3983 Robotruck (DP + 单调队列)
题意:有n个垃圾,第i个垃圾坐标为(xi,yi),重量为wi,有一个机器人,要按照编号从小到大的顺序剑气所有的垃圾兵扔进垃圾桶,垃圾桶在原点, 每次总重量不能超过C,两点间距离为曼哈顿距离,求出最短的 ...