LeetCode Construct Binary Tree from Inorder and Postorder Traversal
2015-05-03 09:35
453 查看
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
题意:中序和后序建树。
思路:还是简单的递归构造。
Note:
You may assume that duplicates do not exist in the tree.
题意:中序和后序建树。
思路:还是简单的递归构造。
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { private TreeNode build(int[] inorder, int[] postorder, int l, int r, int len) { if (len <= 0) return null; int rootVal = postorder[r+len-1]; TreeNode root = new TreeNode(rootVal); int index = 0; for (int i = 0; i < len; i++) if (inorder[l+i] == rootVal) { index = i; break; } root.left = build(inorder, postorder, l, r, index); root.right = build(inorder, postorder, l+index+1, r+index, len-1-index); return root; } public TreeNode buildTree(int[] inorder, int[] postorder) { return build(inorder, postorder, 0, 0, inorder.length); } }
相关文章推荐
- leetcode106. Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode106: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 OJ> 106. 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
- [JAVA]LeetCode106 Construct Binary Tree from Inorder and Postorder Traversal
- leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree f
- leetcode_106_Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal (Medium) (cpp)
- [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 110: Construct Binary Tree from Inorder and Postorder Traversal