Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:虎扑认证过的aj淘宝店 编辑:程序博客网 时间:2024/05/16 06:51
/** * 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) { if (inorder.length != postorder.length) { return null; } return myBuildTree(inorder, 0, inorder.length - 1, postorder, 0, postorder.length - 1); } private TreeNode myBuildTree(int[] inorder, int inBeg, int inEnd, int[] postorder, int postBeg, int postEnd) { if (inBeg > inEnd) { return null; } TreeNode root = new TreeNode(postorder[postEnd]); int location = findLocation(inorder, inBeg, inEnd, postorder[postEnd]); root.left = myBuildTree(inorder, inBeg, location - 1, postorder, postBeg, postBeg + location - inBeg - 1); root.right = myBuildTree(inorder, location + 1, inEnd, postorder, postEnd + location - inEnd, postEnd - 1); return root; } private int findLocation(int[] inorder, int begin, int end, int value) { for (int i = begin; i <= end; i++) { if (inorder[i] == value) { return i; } } return -1; } }
0 0
- *(leetcode) Construct Binary Tree from Inorder and Postorder Traversal (tree)
- LeetCode[Tree]: Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode Construct Binary Tree from Inorder and Postorder Traversal
- [Leetcode] Construct Binary Tree from Inorder and Postorder Traversal
- leetcode 110: Construct Binary Tree from Inorder and Postorder Traversal
- 【leetcode】Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode]Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversal
- Tomcat7 catalina.out 日志分割
- 【Scala】使用Option、Some、None,避免使用null
- JBossClassLoadingUseCases(jboss 类型加载使用案例)
- apktool 反编译 Input file was not found or was not readable.问题解决方案
- Android屏幕适配全攻略(最权威的官方适配指导)
- Construct Binary Tree from Inorder and Postorder Traversal
- Scala 集合
- Android 修改keystore文件密码、alias名称
- C#第七次作业:学习总结
- centos普通用户设置sudo权限
- jquery datatable
- 利用RegSvr32来注册.NET COM组件
- LeetCode Pascal's Triangle
- android5.1 android5.0 themes,styles用法介绍