Leetcode 105.从前序与中序遍历序列构造二叉树
题目
根据一棵树的前序遍历与中序遍历构造出二叉树。
注意:
你可以假设树中没有重复的元素。
例如,给出
1 | 前序遍历 preorder = [3,9,20,15,7] |
返回如下的二叉树:
1 | 3 |
思路
先序遍历:先根节点 后左子树 最后右子树
中序遍历:先左子树 再根节点 最后右子树
所以先序遍历的第一个数值为根节点,在中序遍历中找到根节点位置,前面为左子树的中序遍历,后面为右子树的中序遍历。
Java代码如下:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30/**
* 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==null || inorder==null)
return null;
if(preorder.length==0 || inorder.length==0){
return null;
}
if(preorder.length!=inorder.length){
return null;
}
TreeNode root = new TreeNode(preorder[0]);
int len = preorder.length;
for(int i=0; i<len; i++){
if(inorder[i]==preorder[0]){
root.left = buildTree(Arrays.copyOfRange(preorder,1,i+1), Arrays.copyOfRange(inorder,0,i));
root.right = buildTree(Arrays.copyOfRange(preorder,i+1,len),Arrays.copyOfRange(inorder,i+1,len));
}
}
return root;
}
}
Python代码如下:
1 | # Definition for a binary tree node. |