Leetcode-Construct Binary Tree from inorder and postorder travesal
Given inorder and postorder traversal of a tree, construct the binary tree.
Solution:
/**
* 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.length==0)
return null; int len = inorder.length;
TreeNode root = buildTreeRecur(inorder,postorder,0,len-1,0,len-1);
return root;
} //Build tree for current list, i.e., inorder[inHead] to inorder[inEnd].
public TreeNode buildTreeRecur(int[] inorder, int[] postorder, int inHead, int inEnd, int postHead, int postEnd){
if (inHead==inEnd){
TreeNode root = new TreeNode(inorder[inHead]);
return root;
} int curRoot = postorder[postEnd];
int index = -1;
for (int i=inHead;i<=inEnd;i++)
if (inorder[i]==curRoot){
index = i;
break;
}
int leftNodeNum = index-inHead; int leftInHead = inHead;
int leftInEnd = inHead+leftNodeNum-1;
int rightInHead = index+1;
int rightInEnd = inEnd; int leftPostHead = postHead;
int leftPostEnd = postHead+leftNodeNum-1;
int rightPostHead = leftPostEnd+1;
int rightPostEnd = postEnd-1; TreeNode root = new TreeNode(curRoot);
TreeNode leftChild = null;
if (leftInEnd>=inHead){
leftChild = buildTreeRecur(inorder,postorder,leftInHead,leftInEnd,leftPostHead,leftPostEnd);
root.left = leftChild;
} TreeNode rightChild = null;
if (rightInHead<=inEnd){
rightChild = buildTreeRecur(inorder,postorder,rightInHead,rightInEnd,rightPostHead,rightPostEnd);
root.right = rightChild;
} return root;
}
}
We need to be very carefull about how to count the start and end of the left sub-tree and the right-sub tree. Especially detecting the case that some sub-tree is void.
A better way is to calculate the number of nodes in left and right tree first, then find out the range, like this:
/**
* 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.length==0)
return null; int len = inorder.length;
TreeNode root = buildTreeRecur(inorder,postorder,0,len-1,0,len-1);
return root;
} //Build tree for current list, i.e., inorder[inHead] to inorder[inEnd].
public TreeNode buildTreeRecur(int[] inorder, int[] postorder, int inHead, int inEnd, int postHead, int postEnd){
if (inHead==inEnd){
TreeNode root = new TreeNode(inorder[inHead]);
return root;
} int curRoot = postorder[postEnd];
TreeNode root = new TreeNode(curRoot);
TreeNode leftChild = null;
TreeNode rightChild = null; int index = -1;
for (int i=inHead;i<=inEnd;i++)
if (inorder[i]==curRoot){
index = i;
break;
}
int leftNodeNum = index-inHead;
int rightNodeNum = inEnd-index; if (leftNodeNum>0){
int leftInHead = inHead;
int leftInEnd = inHead+leftNodeNum-1;
int leftPostHead = postHead;
int leftPostEnd = postHead+leftNodeNum-1;
leftChild = buildTreeRecur(inorder,postorder,leftInHead,leftInEnd,leftPostHead,leftPostEnd);
root.left = leftChild;
} if (rightNodeNum>0){
int rightInHead = index+1;
int rightInEnd = inEnd;
int rightPostHead = postEnd-rightNodeNum;
int rightPostEnd = postEnd-1;
rightChild = buildTreeRecur(inorder,postorder,rightInHead,rightInEnd,rightPostHead,rightPostEnd);
root.right = rightChild;
} return root;
}
}
Leetcode-Construct Binary Tree from inorder and postorder travesal的更多相关文章
- [Leetcode] Construct binary tree from inorder and postorder travesal 利用中序和后续遍历构造二叉树
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume th ...
- 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 解题报告
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- [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 @ Python
原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题意: ...
- 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 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 解题报告
[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 ...
随机推荐
- Js日常笔记之数组
1.Array构造函数有一个很大的问题,就是不同的参数,会导致它的行为不一致,es6好像专门为此对数组有升级 因此,不建议使用new Array生成新数组,直接使用数组字面量[...]是更好的做法. ...
- FtpHelper类匿名获取FTP文件
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net ...
- laravel 访问不存在的路由跳转问题!(异常处理)
1.如果你只是想抛出404错误,debug开关可以满足你: 理论上你把 debug 关了,线上环境是会自动到 404 的. 是想要「跳转到 404 页」还是「显示 404 页」?如果是要跳转的话,请配 ...
- andorid HTTPS 不需要证书 VolleyEror: com.android.volley.NoConnectionError: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not fou
1.加证书(这里不说) 2.修改代码 import java.security.KeyManagementException;import java.security.NoSuchAlgorithmE ...
- 查询MySql数据库架构信息:数据库,表,表字段
/*1.查询所有数据库*/ show databases; /*2.查询所有数据表*/ select * from information_schema.tables where table_sch ...
- Linux I/O复用中select poll epoll模型的介绍及其优缺点的比較
关于I/O多路复用: I/O多路复用(又被称为"事件驱动"),首先要理解的是.操作系统为你提供了一个功能.当你的某个socket可读或者可写的时候.它能够给你一个通知.这样当配合非 ...
- 从零開始学android<ScrollView滚动视图.十八.>
因为手机屏幕的高度有限.所以假设面对组件要显示多组信息的时候,ScrollView视图(滚动视图)能够有效的安排这些组件,浏览时能够自己主动的进行滚屏的操作. android.widget.Scrol ...
- 点滴积累【JS】---JS小功能(JS实现匀速运动)
效果: 思路: 利用setInerval()计时器,进行运动.然后关键的一点是在最后停止的时候给它一个填充缝隙的判断. 代码: <head runat="server"> ...
- Android4.4的init进程
1背景 前些日子需要在科室内做关于Android系统启动流程的培训.为此,我在几年前的技术手记的基础上,重新改了一份培训文档.在重新整理文档期间,我也重读了一下Android 4.4的相关代码,发现还 ...
- 李洪强iOS经典面试题32-简单介绍 ARC 以及 ARC 实现的原理
李洪强iOS经典面试题32-简单介绍 ARC 以及 ARC 实现的原理 问题 简单介绍 ARC 以及 ARC 实现的原理. 考查点 ARC 是苹果在 WWDC 2011 提出来的技术,因此很多新入行的 ...