Construct Binary Tree from Inorder and Postorder Traversal(根据中序遍历和后序遍历构建二叉树)
根据中序和后续遍历构建二叉树。
/**
* 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&&postorder==null||(inorder.length==0&&postorder.length==0)) return null;
return build(inorder,0,inorder.length-1,postorder,0,postorder.length-1);
} public TreeNode build(int[] inorder,int inStart,int inEnd,int[] postorder,int postStart,int postEnd){
if(inStart>inEnd||postStart>postStart) return null;
TreeNode root=new TreeNode(postorder[postEnd]); //后续遍历的最后一个是根节点
//在中序遍历中找到根节点的位置
int i;
for( i=inStart;i<=inEnd;i++){
if(inorder[i]==postorder[postEnd]) break;
} root.left=build(inorder,inStart,i-1,postorder,postStart,postStart+i-inStart-1);
root.right=build(inorder,i+1,inEnd,postorder,postStart+i-inStart,postEnd-1);
return root;
}
}
Construct Binary Tree from Inorder and Postorder Traversal(根据中序遍历和后序遍历构建二叉树)的更多相关文章
- [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 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树 C++
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 ...
- Construct Binary Tree from Inorder and Postorder Traversal ——通过中序、后序遍历得到二叉树
题意:根据二叉树的中序遍历和后序遍历恢复二叉树. 解题思路:看到树首先想到要用递归来解题.以这道题为例:如果一颗二叉树为{1,2,3,4,5,6,7},则中序遍历为{4,2,5,1,6,3,7},后序 ...
- Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- 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 ...
- 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 ...
- 【题解二连发】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: Construct Binary Tree from Inorder and Postorder Traversal 解题报告
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
随机推荐
- 06_MyBatis,Spring,SpringMVC整合
项目结构 Spring的配置: beans.xml <?xml version="1.0" encoding="UTF-8"?> <be ...
- [mysql5.6] 主从更换ip之后重新建立同步
情况时这样的: 主从系统 centos6.5 mysql5.6 由于机房迁移ip地址变了,导致原来的主动无法同步,于是需要重新建立主从关系. 主 192.168.1.23 从 192.168.1.22 ...
- iOS中 自定义系统相机 作者:韩俊强
需要框架: #import <AVFoundation/AVFoundation.h> #import <AssetsLibrary/AssetsLibrary.h> 布局如下 ...
- volatile适用场合
volatile适用场合 要在多线程中安全的适用volatitle变量,必须同时满足: 1.对变量的写入操作不依赖其当前值 不满足:number++.count = count * 5等 ...
- UML 类图. 对象图. 接口图. 用例图 .包,参与者. 依赖关系. 泛化/继承关系. 关联关系 .聚合/聚集关系. 实现关系 组合关系。
结构元素 结构元素包括,类,对象,接口,用例,参与者. 类图 类图图示 类图是UML中最基本的元素了吧?根据OO的思想"天下一切皆对象",而类是对象的抽象. 左 ...
- 谈谈Ext JS的组件——布局的使用方法续二
绝对布局(Ext.layout.container.Absolute) 绝对布局让我回想到了使用Foxpro开发的时候,哪时候的界面布局就是这样,通过设置控件的左上角坐标(x,y)和宽度来进行的,因为 ...
- JAVA之旅(九)——Object类,equals,toString,getClass,内部类访问规则,静态内部类,内部类原则,匿名内部类
JAVA之旅(九)--Object类,equals,toString,getClass,内部类访问规则,静态内部类,内部类原则,匿名内部类 天天被一些琐事骚扰,学习还得继续 一.Object类 Obj ...
- Django介绍、安装配置、基本使用、Django用户注册例子
Django介绍 Django 是由 Python 开发的一个免费的开源网站框架,可以用于快速搭建高性能,优雅的网站 DjangoMTV的思想 没有controller ...
- SpriteBuilder中应用智能精灵集之后提示找不到文件的解决
SpriteBuilder中有一个将方便的功能,可以用文件夹中的若干图片生成1张图片;这称之为智能精灵集合(smart sprite sheet).好处是可以一次性的加载图片到显存中去,提升了性能. ...
- 4种方法让SpringMVC接收多个对象(转:http://blog.csdn.net/lutinghuan/article/details/46820023)
问题背景: 我要在一个表单里同时一次性提交多名乘客的个人信息到SpringMVC,前端HTML和SpringMVC Controller里该如何处理? 第1种方法:表单提交,以字段数组接收: 第2种方 ...