LeetCode - 106. Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:雅思词汇书推荐 知乎 编辑:程序博客网 时间:2024/04/28 04:01
这道题目的思路与上一题基本是一样的,就不再详细写出,代码如下:
/** * 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 buildTreeInPost(int preEnd, int inStart, int inEnd, int[] inorder, int[] postorder){ if(preEnd < 0 || inStart > inEnd) return null; TreeNode root = new TreeNode(postorder[preEnd]); int inIndex = 0; // index of root in postorder for(int i = inStart; i <= inEnd; i++){ if(inorder[i] == root.val) inIndex = i; } root.left = buildTreeInPost(preEnd - (inEnd - inIndex) - 1, inStart, inIndex - 1, inorder, postorder); root.right = buildTreeInPost(preEnd - 1, inIndex + 1, inEnd, inorder, postorder); return root; } public TreeNode buildTree(int[] inorder, int[] postorder) { return buildTreeInPost(postorder.length - 1, 0, inorder.length - 1, inorder, postorder); }}
0 0
- 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
- [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
- 如何获取已加载在ImageView中的图片ID
- J2SE--基础语法一
- static struct option opts[] =
- swiper的基础使用(五)
- linux---deepin下 git的使用
- LeetCode - 106. Construct Binary Tree from Inorder and Postorder Traversal
- windows 10 bash change PS1
- Android--启动另一个Activity
- swiper的基础使用(六)
- 回溯法
- swiper的基础使用(七)
- Irrlicht学习笔记(6)--2DGraphics
- redis 数据类型
- Linux 中配置环境变量等问题