LeetCode解题 105:Construct Binary Tree from Preorder and Inorder Traversal
Problem 105: Construct Binary Tree from Preorder and Inorder Traversal [Medium]
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
For example, given
preorder = [3,9,20,15,7]
inorder = [9,3,15,20,7]
Return the following binary tree:
3
/ \
9 20
/ \
15 7
来源:LeetCode
解题思路
使用前序遍历和中序遍历可以唯一构建一棵二叉树,只需要在中序遍历的数组中找到前序遍历中的根节点,就可以划分左子树和右子树范围,从而递归构建二叉树。
具体过程:
- 首先构建根节点数值,即preorder[0]:
r o o t = T r e e N o d e ( p r e o r d e r [ 0 ] ) root = TreeNode(preorder[0]) root=TreeNode(preorder[0]) - 然后遍历inorder数组,找到inorder[index] = preorder[0],
index即为根节点在inorder中的下标。 - 构建左子树的前序遍历和中序遍历数组:
l C h i l d p r e = c o p y ( p r e o r d e r [ 1 ] , . . . , p r e o r d e r [ i n d e x ] ) l C h i l d i n = c o p y ( i n o r d e r [ 0 ] , . . . , i n o r d e r [ i n d e x − 1 ] ) lChildpre = copy(preorder[1],...,preorder[index]) \\ lChildin = copy(inorder[0],...,inorder[index-1]) lChildpre=copy(preorder[1],...,preorder[index])lChildin=copy(inorder[0],...,inorder[index−1]) - 同样可以构建右子树相应的数组:
r C h i l d p r e = c o p y ( p r e o r d e r [ i n d e x + 1 ] , . . . , p r e o r d e r [ e n d − 1 ] ) r C h i l d i n = c o p y ( i n o r d e r [ i n d e x + 1 ] , . . . , i n o r d e r [ e n d − 1 ] ) rChildpre = copy(preorder[index+1],...,preorder[end-1]) \\ rChildin = copy(inorder[index+1],...,inorder[end-1]) rChildpre=copy(preorder[index+1],...,preorder[end−1])rChildin=copy(inorder[index+1],...,inorder[end−1]) - 递归构建当前根节点的左右子树:
r o o t . l e f t = b u i l d T r e e ( l C h i l d p r e , l C h i l d i n ) r o o t . r i g h t = b u i l d T r e e ( r C h i l d p r e , r C h i l d i n ) root.left = buildTree(lChildpre, lChildin) \\ root.right = buildTree(rChildpre, rChildin) root.left=buildTree(lChildpre,lChildin)root.right=buildTree(rChildpre,rChildin)
要点:递归
Solution (Java)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
if(preorder.length < 1) return null;
if(preorder.length == 1) return new TreeNode(preorder[0]);
int index = 0;
TreeNode root = new TreeNode(preorder[0]);
for(int i = 0; i < inorder.length; i++){
if(inorder[i] == preorder[0]){
index = i;
break;
}
}
// left Child
int[] lChildpre = new int[index];
System.arraycopy(preorder, 1, lChildpre, 0, index);
int[] lChildin = new int[index];
System.arraycopy(inorder, 0, lChildin, 0, index);
root.left = buildTree(lChildpre, lChildin);
// right Child
int rlen = preorder.length - index - 1;
int[] rChildpre = new int[rlen];
System.arraycopy(preorder, index + 1, rChildpre, 0, rlen);
int[] rChildin = new int[rlen];
System.arraycopy(inorder, index + 1, rChildin, 0, rlen);
root.right = buildTree(rChildpre, rChildin);
return root;
}
}
本文介绍如何使用前序遍历和中序遍历构建二叉树的方法,通过递归查找根节点并划分左右子树,最终实现树的构建。提供了详细的解题思路和Java代码实现。
446

被折叠的 条评论
为什么被折叠?



