LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告
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.
SOLUTION 1:
/**
* 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[] inorder, int[] postorder) {
if (inorder == null || postorder == null) {
return null;
} return dfs(inorder, postorder, 0, inorder.length - 1, 0, postorder.length - 1);
} public TreeNode dfs(int[] inorder, int[] postorder, int inL, int inR, int postL, int postR) {
if (inL > inR) {
return null;
} // create the root node.
TreeNode root = new TreeNode(postorder[postR]); // find the position of the root node in the inorder traversal.
int pos = 0;
for (; pos <= inR; pos++) {
if (inorder[pos] == postorder[postR]) {
break;
}
} int leftNum = pos - inL; root.left = dfs(inorder, postorder, inL, pos - 1, postL, postL + leftNum - 1);
root.right = dfs(inorder, postorder, pos + 1, inR, postL + leftNum, postR - 1); return root;
}
}
代码: https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/tree/BuildTree2.java
LeetCode: 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: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 Inorder and Postorder Traversal 由中序和后序遍历建立二叉树
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...
- 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 ...
- LeetCode——Construct Binary Tree from Inorder and Postorder Traversal
Question Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may a ...
- [leetcode]Construct Binary Tree from Inorder and Postorder Traversal @ Python
原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题意: ...
- 【LeetCode】889. Construct Binary Tree from Preorder and Postorder Traversal 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
随机推荐
- kafka负载均衡相关资料收集(三)
apache kafka系列之Producer处理逻辑 下文是转载的,原文链接地址:点这儿 [转] Kafka ProducerKafka Producer处理逻辑kafka生产者处理逻辑apache ...
- SoapUI利用Groovy把外部数据加载到request中
默认已经用Groovy把外部数据给读取出来了,关键是读取出来后,如何加载到request中去?这里提供了两种方法:1.该Groovy脚本的名称是"setUp" def num = ...
- 纯CSS兑现侧边栏/分栏高度自动相等(转)
这里直接介绍我认为的最佳的侧边栏/分栏高度自动相等方法.核心的CSS代码如下(数值不固定): margin-bottom:-3000px; padding-bottom:3000px; 再配合父标签的 ...
- YY老总李学凌给记者们的几句话
从记者到总编,从狗狗.多玩到如今的 YY.100 教育,似乎李学凌在这么多年来一直没有放缓过脚步.作为记者转型的成功案例,李学凌总结记者生涯有几方面令其获益匪浅: 1.平常心.对待再高层次的人,也用一 ...
- 微信图片分享遇到 checkArgs fail, thumbData is invalid
该问题主要是微信图片限制32K以内的原因,可将bmpToByteArray方法进行进行改写. 原方法是: /** * 得到Bitmap的byte * @author netcorner * @para ...
- 数字的可视化:python画图之散点图sactter函数详解
最近开始学习python编程,遇到scatter函数,感觉里面的参数不知道什么意思于是查资料,最后总结如下: 1.scatter函数原型 2.其中散点的形状参数marker如下: 3.其中颜色参数c如 ...
- process credentials(一)
一.介绍 当linux系统中的一个进程运行起来的时候,总是要访问系统的资源,访问文件或者向其他的进程发送信号.系统是否允许其进行这些操作?系统是根据什么来判断该进程的权限?这些问题是和进程信任状(pr ...
- 探索MVP(Model-View-Presenter)设计模式在SharePoint平台下的实现
对于SharePoint Developers来说,往往会过多的去关注SharePoint平台和工具,而把设计模式和代码的可测试性放在了一个较低的优先级.这并不是说SharePoint Develop ...
- Groovy 学习手册(1)
1. 需要安装的软件 Java / Groovy 对应 Java 和 Groovy,你需要安装以下软件: Java JDK,例如 JDK 8 IDE,例如 Eclipse,NetBeans 8 Gro ...
- x电容和Y电容
https://wenku.baidu.com/view/c0a68a6a4a7302768e9939bd.html 根據 IEC 60384-14, 電容器分為 X 電容及 Y 電容 , 1. X ...