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 ...
随机推荐
- [C]simple code of count input lines,words,chars
This is a simple C program which can count input lines, words and chars. But the number of words are ...
- 【嵌入式开发】 Bootloader 详解 ( 代码环境 | ARM 启动流程 | uboot 工作流程 | 架构设计)
作者 : 韩曙亮 博客地址 : http://blog.csdn.net/shulianghan/article/details/42462795 转载请著名出处 相关资源下载 : -- u-boo ...
- JavaScript 关键字
JavaScript 关键字 和其他任何编程语言一样,JavaScript 保留了一些关键字为自己所用. JavaScript 同样保留了一些关键字,这些关键字在当前的语言版本中并没有使用,但在以后 ...
- MOAC中“MO:安全性配置文件“对于开发者
1. 获取配置文件的值:应用开发员->配置文件->输入用户配置文件名,找到上面的名称,即可填入fnd_profile.VALUE()中. 2. MO:安全性配置文件有值的话,就代表启用了M ...
- design principle:java 回调与委派/委托机制
博客 design principle:模拟 android Button 控件点击事件 主要说了一下模拟 android 的 Listener 模式,其实这就是一种委派与回调机制的体现. 委派,也可 ...
- iOS编程中的音频知识(二):那么多种格式我应该用哪一个?
iPhone支持不少格式,比如AAC,HE-AAC,AMR,IMA4等等,你可以在以下网址看到比较全的格式和简要介绍: http://www.raywenderlich.com/69365/audio ...
- 字符转码开源库libiconv目前还不支持64位
最新版的libiconv 1.14目前还不支持64位系统,只能编译出32位库. libiconv 1.14下载地址: http://ftp.gnu.org/pub/gnu/libiconv/libic ...
- 【翻译】对于Ext JS 5,你准备好了吗?
原文:Are You Ready for Ext JS 5? Ext JS 5:准备升级 对于Ext JS 5加入Sencha的大家庭,我们感到非常高兴!作为一个主要版本,在Ext JS 5引入了一堆 ...
- 《java第一季之入门篇》的想法
学习java也有一段时间了,但是考虑到自己现在上课.复习.考试等耗费很多时间,感觉没有静下心来的时间去写一个长期的博客.计划今年7月1号开始写一套关于java的入门篇博客文章,入门篇计划这样--涵盖j ...
- 【65】Mybatis详解
Mybatis介绍 MyBatis是一款一流的支持自定义SQL.存储过程和高级映射的持久化框架.MyBatis几乎消除了所有的JDBC代码,也基本不需要手工去设置参数和获取检索结果.MyBatis能够 ...