您的位置:首页 > 其它

Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal

2016-03-11 05:42 489 查看
105. Construct Binary Tree from Preorder and Inorder Traversal

Total Accepted: 58238 Total
Submissions: 206790 Difficulty: 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.

分析:

1
/ \
2   3
/ \ / \
4  5 6  7
inorder:       [4,2,5,1,6,3,7]
preorder:      [1,2,4,5,3,6,7]
postorder:     [4,5,2,6,7,3,1]


preorder的第一个节点是整个树的根,下一个节点便是root的左节点,而root的右节点需要知道root左子节点的个数,因此需要在inorder序列中找

该节点的index,则index-instart是左子节点的数目,加1算入root节点,这就是root右节点在preorder中相对于当前位置prestart的偏移量。

Method 1: 普通方法,遍历inorder数组寻找 目标index,然后分别调用递归赋值左右子节点。

public class Solution{ //23ms
public TreeNode buildTree(int[] preorder, int[] inorder) {
return helper(0, 0, inorder.length - 1, preorder, inorder);
}

public TreeNode helper(int preStart, int inStart, int inEnd, int[] preorder, int[] inorder) {
if (preStart > preorder.length - 1 || inStart > inEnd) {
return null;
}
TreeNode root = new TreeNode(preorder[preStart]);
int inIndex = 0; // Index of current root in inorder
for (int i = inStart; i <= inEnd; i++) {
if (inorder[i] == root.val) {
inIndex = i;
}
}
root.left = helper(preStart + 1, inStart, inIndex - 1, preorder, inorder);
root.right = helper(preStart + inIndex - inStart + 1, inIndex + 1, inEnd, preorder, inorder);
return root;
}
}


Method 2: 网上的speed up版本,用了hashmap提高速度。

public class Solution{ //6ms
public TreeNode buildTree(int[] preorder, int[] inorder) {
Map<Integer, Integer> inMap = new HashMap<Integer, Integer>();

for(int i = 0; i < inorder.length; i++) {
inMap.put(inorder[i], i);
}

TreeNode root = buildTree(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1, inMap);
return root;
}

public TreeNode buildTree(int[] preorder, int preStart, int preEnd, int[] inorder, int inStart, int inEnd, Map<Integer, Integer> inMap) {
if(preStart > preEnd || inStart > inEnd) return null;

TreeNode root = new TreeNode(preorder[preStart]);
int inRoot = inMap.get(root.val);
int numsLeft = inRoot - inStart;

root.left = buildTree(preorder, preStart + 1, preStart + numsLeft, inorder, inStart, inRoot - 1, inMap);
root.right = buildTree(preorder, preStart + numsLeft + 1, preEnd, inorder, inRoot + 1, inEnd, inMap);

return root;
}
}


Method 3: 发现网上最快的2MS其实就是用了双向指针加速,并且找到index后直接break,于是改进了method 1。

public class Solution{ //2ms
public TreeNode buildTree(int[] preorder, int[] inorder) {
return helper(0, 0, inorder.length - 1, preorder, inorder);
}

public TreeNode helper(int preStart, int inStart, int inEnd, int[] preorder, int[] inorder) {
if (preStart > preorder.length - 1 || inStart > inEnd) {
return null;
}
TreeNode root = new TreeNode(preorder[preStart]);
int inIndex = 0; // Index of current root in inorder
for (int i = inStart, j=inEnd; i <= j; i++,j--) {
if (inorder[i] == root.val) {
inIndex = i;
break;
}else if(inorder[j] == root.val){
inIndex=j;
break;
}
}
root.left = helper(preStart + 1, inStart, inIndex - 1, preorder, inorder);
root.right = helper(preStart + inIndex - inStart + 1, inIndex + 1, inEnd, preorder, inorder);
return root;
}
}


因为标题不能过长,所以106. Construct Binary Tree from Inorder and Postorder Traversal 重开一文。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: