leetcode -- Construct Binary Tree from Preorder and Inorder Traversal
2013-08-23 10:33
375 查看
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
[解题思路]
由前序遍历知第一个节点是根节点,根据此节点值去中序遍历集合中找该root值所处位置,
该位置之前的数属于左子树,之后的属于右子树,即找到左子树和右子树所处的子序列,接下来就可以用递归来完成
递归的终止条件:preorder中仅有一个元素且preorder中的元素和inorder中元素相同
Note:
You may assume that duplicates do not exist in the tree.
[解题思路]
由前序遍历知第一个节点是根节点,根据此节点值去中序遍历集合中找该root值所处位置,
该位置之前的数属于左子树,之后的属于右子树,即找到左子树和右子树所处的子序列,接下来就可以用递归来完成
递归的终止条件:preorder中仅有一个元素且preorder中的元素和inorder中元素相同
/** * @author rgc * @date 2013-8-23上午10:07:40 * @version 1.0 */ public class Solution { public static void main(String[] args) { int[] preorder = new int[]{1,2,3}; int[] inorder = new int[]{1,2,3}; buildTree(preorder, inorder); } public static TreeNode buildTree(int[] preorder, int[] inorder) { // Start typing your Java solution below // DO NOT write main() function if (preorder == null || inorder == null) { return null; } int preLen = preorder.length; int inLen = inorder.length; if (preLen == 0 || inLen == 0) { return null; } return constructTree(preorder, 0, preLen - 1, inorder, 0, inLen - 1); } public static TreeNode constructTree(int[] preorder, int preStart, int preEnd, int[] inorder, int inStart, int inEnd) { int rootValue = preorder[preStart]; TreeNode root = new TreeNode(rootValue); root.left = null; root.right = null; if (preStart == preEnd && preorder[preStart] == inorder[inStart]) { return root; } int i = inStart; for (; i <= inEnd; i++) { if (rootValue == inorder[i]) { break; } } int leftLen = i - inStart; // exsit left subtree if (leftLen > 0) { root.left = constructTree(preorder, preStart + 1, preStart + leftLen, inorder, inStart, i - 1); } if (inEnd > i) { root.right = constructTree(preorder, preStart + leftLen + 1, preEnd, inorder, i + 1, inEnd); } return root; } } class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } }
相关文章推荐
- [LeetCode]105 Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode - Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode-105. Construct Binary Tree from Preorder and Inorder Traversal
- [Leetcode] Construct Binary Tree from Preorder and Inorder Traversal (Java)
- LeetCode: Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal leetcode java
- leetcode[105]Construct Binary Tree from Preorder and Inorder Traversal
- leetcode 109: Construct Binary Tree from Preorder and Inorder Traversal
- [Leetcode][python]Construct Binary Tree from Preorder and Inorder Traversal (Inorder and Postorder)
- [LeetCode]Construct Binary Tree from Preorder and Inorder Traversal
- leetcode——105——Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode_Construct Binary Tree from Preorder and Inorder Traversal
- leetcode105~Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode]Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode – Refresh – Construct Binary Tree from Inorder and Preorder Traversal
- leetcode笔记:Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode]105. Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode] 021: Construct Binary Tree from Preorder and Inorder Traversal