leetcode 105 Construct Binary Tree from Preorder and Inorder Traversal
2018-03-14 14:46
453 查看
Java:class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
return build(preorder, 0, inorder, 0, inorder.length-1);
}
public TreeNode build(int[] preorder, int pre_str, int[] inorder, int in_str, int in_end){
if(pre_str > preorder.length-1 || in_str > in_end) return null;
int index = 0;
TreeNode root = new TreeNode(preorder[pre_str]);
for(int i=in_str; i<=in_end; i++){
if(inorder[i] == root.val){
index = i;
break;
}
}
root.left = build(preorder, pre_str+1, inorder, in_str, index-1);
root.right = build(preorder, pre_str+index-in_str+1, inorder, index+1, in_end);
return root;
}
}Python:class Solution:
def buildTree(self, preorder, inorder):
"""
:type preorder: List[int]
:type inorder: List[int]
:rtype: TreeNode
"""
if inorder:
beg = preorder.pop(0)
#.index是否包含字符
ind = inorder.index(beg)
root = TreeNode(inorder[ind])
root.left = self.buildTree(preorder, inorder[0:ind])
root.right = self.buildTree(preorder, inorder[ind+1:])
return root
public TreeNode buildTree(int[] preorder, int[] inorder) {
return build(preorder, 0, inorder, 0, inorder.length-1);
}
public TreeNode build(int[] preorder, int pre_str, int[] inorder, int in_str, int in_end){
if(pre_str > preorder.length-1 || in_str > in_end) return null;
int index = 0;
TreeNode root = new TreeNode(preorder[pre_str]);
for(int i=in_str; i<=in_end; i++){
if(inorder[i] == root.val){
index = i;
break;
}
}
root.left = build(preorder, pre_str+1, inorder, in_str, index-1);
root.right = build(preorder, pre_str+index-in_str+1, inorder, index+1, in_end);
return root;
}
}Python:class Solution:
def buildTree(self, preorder, inorder):
"""
:type preorder: List[int]
:type inorder: List[int]
:rtype: TreeNode
"""
if inorder:
beg = preorder.pop(0)
#.index是否包含字符
ind = inorder.index(beg)
root = TreeNode(inorder[ind])
root.left = self.buildTree(preorder, inorder[0:ind])
root.right = self.buildTree(preorder, inorder[ind+1:])
return root
相关文章推荐
- LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal LeetCode 105
- [LeetCode]*105.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 | 根据前序和中序遍历构建二叉树 | Medium
- leetcode || 105、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
- LeetCode105 Construct Binary Tree from Preorder and Inorder Traversal
- 【LeetCode】105 & 106 Construct Binary Tree from (Preorder and Inorder) || (Inorder and Postorder)Traversal
- Leetcode[105]-Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode]题解(python):105-Construct Binary Tree from Preorder and Inorder Traversal
- C++详解Leetcode:105. Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode-105:Construct Binary Tree from Preorder and Inorder Traversal (利用先序和中序遍历构建二叉树) -- medium
- [LeetCode#105]Construct Binary Tree from Preorder and Inorder Traversal
- 【LeetCode-105】Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode105 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——105——Construct Binary Tree from Preorder and Inorder Traversal