输入某二叉树的前序遍历和中序遍历的结果,请重建该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
例如,给出
前序遍历 preorder = [3,9,20,15,7]
中序遍历 inorder = [9,3,15,20,7]
返回如下的二叉树:
3
/ \
9 20
/ \
15 7
限制:
0 <= 节点个数 <= 5000
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
class Solution { HashMap<Integer, Integer> map = new HashMap<>(); int[] preorder;
public TreeNode buildTree(int[] preorder, int[] inorder) { this.preorder = preorder; for (int i = 0; i < preorder.length; i++) { map.put(inorder[i], i); } return recursive(0,0,inorder.length-1); }
public TreeNode recursive(int pre_root_idx, int in_left_idx, int in_right_idx) { if (in_left_idx > in_right_idx) { return null; } TreeNode root = new TreeNode(preorder[pre_root_idx]); int idx = map.get(preorder[pre_root_idx]);
root.left = recursive(pre_root_idx + 1, in_left_idx, idx - 1);
root.right = recursive(pre_root_idx + (idx-1 - in_left_idx +1) + 1, idx + 1, in_right_idx); return root; } }
|