Given inorder and postorder traversal of a tree, construct the binary tree.

Note:
You may assume that duplicates do not exist in the tree.

和上一道题基本一样

根据中序和后序遍历确定一个棵树。

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode buildTree(int[] inorder, int[] postorder) { int len = inorder.length;
if( len == 0)
return null; return helper(inorder,0,len-1,postorder,len-1); } public TreeNode helper(int[] inorder,int start,int end, int[] postorder, int pos ){ if( pos < 0 || start > end)
return null;
TreeNode node = new TreeNode(postorder[pos]); int size = 0;
for( int i = end;i >= start && inorder[i] != postorder[pos];i--,size++)
; node.left = helper(inorder,start,end-size-1,postorder,pos-size-1); node.right = helper(inorder,end-size+1,end,postorder,pos-1); return node; }
}

leetcode 106 Construct Binary Tree from Inorder and Postorder Traversal----- java的更多相关文章

  1. 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: ...

  2. (二叉树 递归) 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 ...

  3. [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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. Leetcode#106 Construct Binary Tree from Inorder and Postorder Traversal

    原题地址 二叉树基本操作 [       ]O[              ] [       ][              ]O 代码: TreeNode *restore(vector<i ...

  8. [leetcode] 106. Construct Binary Tree from Inorder and Postorder Traversal(medium)

    原题地址 思路: 和leetcode105题差不多,这道题是给中序和后序,求出二叉树. 解法一: 思路和105题差不多,只是pos是从后往前遍历,生成树顺序也是先右后左. class Solution ...

  9. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    [LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...

  10. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

随机推荐

  1. cmd打开控制面板及其他命令

    如果你在权限较小的域用户的机器上,要做一些管理操作,就不可避免的要使用cmd打开一些以前只能在图形界面里打开的程序.下面是我收集的一些常用操作. 以某个身份启动程序:runas /user:it\n1 ...

  2. 使用 JDBC 调用函数 & 存储过程

    /** * 如何使用 JDBC 调用存储在数据库中的函数或存储过程 */ @Test public void testCallableStatment() { Connection connectio ...

  3. @ResultMapping注解

    @RequestMapping注解1.url映射放在方法上:@RequestMapping("/itemsEdit")2.窄化url请求映射放在类上,定义根路径,url就变成根路径 ...

  4. ios 8+ (xcode 6.0 +)应用程序Ad Hoc 发布前多设备测试流程详解

    我们开发的程序在经过simulator以及自己的iOS设备测试后,也基本完成应用程序了,这时候我们就可以把它发布出去了更更多的人去测试,我们可以在iOS平台使用ad hoc实现. 你在苹果购买的开发者 ...

  5. 嵌套遍历<s:iterator>map=new TreeMap(string,Map(string,User))

    //嵌套遍历,先给外层的map(假设是放在root中的,如果放在context的map中,要加#)取个别名,放到Actioncontext中 <s:iterator value="ma ...

  6. Java基础毕向东day04

    1. 数组 2.选择排序.冒泡排序.折半查找.

  7. C++11 std::copy

    这个函数并不是简单的 while(first != last) { *result = *first; result++; first++; } 事实上这种写法是最具普适性的,值要求inputIter ...

  8. 学习:Log中'main', 'system', 'radio', 'events'

    在Android中不同的log写到不同的设备中,共有/dev/log/system, /dev/log/main, /dev/log/radion, /dev/log/events四中类型.其中默认L ...

  9. Git ~ 管理修改 ~ Gitasd

    现在假设你一经常我了暂存区的概念 , 下面我们将要讨论的就是 , 为什么 Git 比其他的版本控制系统设计的优秀 , 因为 Git 跟踪管理的是修改而非文件 什么是修改  ? 修改就是 你在某个地方 ...

  10. 渐变背景 css3渐变效果及代码

    渐变背景及代码  http://uigradients.com/#Behongo