Construct Binary Tree from Inorder and Postorder Traversal

Total Accepted: 31041 Total Submissions: 115870

 
 

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

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

解题思路:

修改下上题代码解决,JAVA实现如下:

	static public TreeNode buildTree(int[] inorder, int[] postorder) {
return buildTree(postorder, inorder, 0, postorder.length-1, 0, inorder.length-1);
}
static public TreeNode buildTree(int[] postorder, int[] inorder, int pBegin, int pEnd, int iBegin, int iEnd){
if(pBegin>pEnd)
return null;
TreeNode root = new TreeNode(postorder[pEnd]);
int i = iBegin;
for(;i<iEnd;i++)
if(inorder[i]==root.val)
break;
int lenLeft = i-iBegin;
root.left = buildTree(postorder, inorder, pBegin, pBegin+lenLeft-1, iBegin, i-1);
root.right = buildTree(postorder, inorder, pBegin+lenLeft, pEnd-1, i+1, iEnd);
return root;
}

Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal的更多相关文章

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

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

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

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

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

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

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

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

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

  9. 【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. andriod GridLayout

    来自:http://blog.csdn.net/jianghuiquan/article/details/8299973 GridLayout网格布局 android4.0以上版本出现的GridLay ...

  2. OWASP Dependency-Check插件介绍及使用

    1.Dependency-Check可以检查项目依赖包存在的已知.公开披露的漏洞.目前良好的支持Java和.NET:Ruby.Node.js.Python处于实验阶段:仅支持通过(autoconf a ...

  3. 【Java编程】JDBC注入攻击-Statement 与 PreparedStatement

    在上一篇[Java编程]建立一个简单的JDBC连接-Drivers, Connection, Statement and PreparedStatement我们介绍了怎样使用JDBC驱动建立一个简单的 ...

  4. Linux常用的几个vi小命令

    输入跳转命令: 命令行前 Ctrl+A 命令行后 Ctrl+E VI命令中: 当前行 行首  "0" 当前行 行尾  "Shift+4" 当前文档首行首字符:& ...

  5. 2016.7.14 去掉Mybatis Generator生成的一堆 example

    参考资料: http://www.cnblogs.com/zerocold/p/4220955.html   mybatis generator自动生成的代码里老是有一堆example,需要改的时候, ...

  6. Mockito 库、powermock扩展

    转载:http://blog.csdn.net/kittyboy0001/article/details/18709685 Mockito 简介 Mockito 是目前 java 单测中使用比较流行的 ...

  7. sql执行顺序图

    http://www.16aspx.com/cmsimages/20130325/664845013.png

  8. odoo图片显示

        如果在odoo客户端展示图片, 可以用 url( data:image/png;base64, 图片base64编码过的内容) 展示, 例如     url(data:image/png;ba ...

  9. HDU5294——Tricks Device(最短路 + 最大流)

    第一次做最大流的题目- 这题就是堆模板 #include <iostream> #include <algorithm> #include <cmath> #inc ...

  10. 关于顺序磁盘IO比内存随机IO快的讨论

    这个问题来源于我书中引用的一幅图: 我们从图中明显可以看某性能测试的结果表明普通机械磁盘的顺序I/O性能指标是53.2M values/s,SSD的顺序I/O性能指标是42.2M values/s,而 ...