[leetcode]106. Construct Binary Tree from Inorder and Postorder Traversal@Java结题报告
来源:互联网 发布:sql触发器实例 old 编辑:程序博客网 时间:2024/06/06 01:31
https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/description/
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
package go.jacob.day807;/** * 106. Construct Binary Tree from Inorder and Postorder Traversal * * @author Jacob * */public class Demo1 {public TreeNode buildTree(int[] inorder, int[] postorder) {if (inorder == null)return null;return buildTree(inorder, 0, inorder.length - 1, postorder, 0, postorder.length - 1);}private TreeNode buildTree(int[] inorder, int inLeft, int inRight, int[] postorder, int postLeft, int postRight) {if (inLeft > inRight)return null;TreeNode root = new TreeNode(postorder[postRight]);// 只有一个节点,返回rootif (inLeft == inRight)return root;int rootNum = 0;for (int i = inLeft; i <= inRight; i++) {if (inorder[i] == postorder[postRight]) {rootNum = i;break;}}int leftLength = rootNum - inLeft;//递归左子树和右子树root.left = buildTree(inorder, inLeft, inLeft + leftLength - 1, postorder, postLeft, postLeft + leftLength - 1);root.right = buildTree(inorder, inLeft + leftLength + 1, inRight, postorder, postLeft + leftLength,postRight - 1);return root;}}
阅读全文
0 0
- [leetcode]106. Construct Binary Tree from Inorder and Postorder Traversal@Java结题报告
- [Leetcode] Construct Binary Tree from Inorder and Postorder Traversal (Java)
- [LeetCode][Java] Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal
- [Leetcode] 106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode --- 106. Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode]*106.Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode] 106.Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode#106. Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode *** 106. Construct Binary Tree from Inorder and Postorder Traversal
- 【leetcode】106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode-106.Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode - 106. Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode] 106. Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode] 106. Construct Binary Tree from Inorder and Postorder Traversal
- form的onsubmit事件--表单提交前的验证最佳实现方式
- 高效率的c++之如何分析网络数据包之struct关键技术
- POJ2065-SETI
- 一个很好用的模板(截取网络数据包)
- Kendo UI 使用札记
- [leetcode]106. Construct Binary Tree from Inorder and Postorder Traversal@Java结题报告
- MyEclipse10安装SVN插件
- vue 后台数据获取与组件渲染、页面刷新数据消失的问题小结
- 算法题目---和为s的两个数字vs和为s的连续正数序列
- JPanel、JScrollPane 面板详解
- 学习C++中的CustomDraw
- CUSTOMDRAW msdn网站
- 停车场(2)
- linux进行vi操作时的撤销操作