Advertisement
sweet1cris

Untitled

Sep 25th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.86 KB | None | 0 0
  1. public class ReconstructBTInPre {
  2.   // Method 1: Utilizing the inOrder sequence to determine
  3.   // the size of left/right subtrees.
  4.   public TreeNode reconstruct(int[] in, int[] pre) {
  5.     // Assumptions: pre, in are not null, there is no duplicates
  6.     // in the binary tree, the length of pre an in are guaranteed
  7.     // to be the same.
  8.     Map<Integer, Integer> inIndex = indexMap(in);
  9.     return helper(pre, inIndex, 0, in.length - 1, 0, pre.length - 1);
  10.   }
  11.  
  12.   private Map<Integer, Integer> indexMap(int[] in) {
  13.     Map<Integer, Integer> map = new HashMap<>();
  14.     for (int i = 0; i < in.length; i++) {
  15.       map.put(in[i], i);
  16.     }
  17.     return map;
  18.   }
  19.  
  20.   private TreeNode helper(int[] pre, Map<Integer, Integer> inIndex, int inLeft,
  21. int inRight, int preLeft, int preRight) {
  22.     if (inLeft > inRight) {
  23.       return null;
  24.     }
  25.     TreeNode root = new TreeNode(pre[preLeft]);
  26.     // get the position of the root in inOrder sequence, so that we will know
  27.     // the size of left/right subtrees.
  28.     int inMid = inIndex.get(root.key);
  29.     root.left = helper(pre, inIndex, inLeft, inMid - 1, preLeft + 1, preLeft +
  30. inMid - inLeft);
  31.     root.right = helper(pre, inIndex, inMid + 1, inRight, preRight + inMid -
  32. inRight + 1, preRight);
  33.     return root;
  34.   }
  35.  
  36.   // Method 2: Another Linear Solution with traversing and constructing
  37.   // the binary tree using preOrder and inOrder at the same time.
  38.   public TreeNode reconstructII(int[] in, int[] pre) {
  39.     // Assumptions: pre, in are not null, there is no duplicates
  40.     // in the binary tree, the length of pre an in are guaranteed
  41.     // to be the same.
  42.     int[] preIndex = new int[] { 0 };
  43.     int[] inIndex = new int[] { 0 };
  44.     return helperII(pre, in, preIndex, inIndex, Integer.MAX_VALUE);
  45.   }
  46.  
  47.   private TreeNode helperII(int[] pre, int[] in, int[] preIndex, int[] inIndex,
  48. int target) {
  49.     // Traversing and construct the binary tree using preOrder and inOrder,
  50.     // the preOrder is [root][left subtree][right subtree]
  51.     // from the preOrder, we know the root of the binary tree,
  52.     // the inOrder is [left subtree][root][right subtree]
  53.     // when we know the root, we actually know the boundary of
  54.     // the left/right subtree.
  55.     // The "target" is actually the root, and we are using inOrder
  56.     // to identify the boundary of left subtree.
  57.     if (inIndex[0] >= in.length || in[inIndex[0]] == target) {
  58.       return null;
  59.     }
  60.     TreeNode root = new TreeNode(pre[preIndex[0]]);
  61.  
  62.     // preOrder, advance the index by 1 since we already finish the root.
  63.     preIndex[0]++;
  64.     root.left = helperII(pre, in, preIndex, inIndex, root.key);
  65.     // inOrder, after finish the left subtree, we can advance the index by 1.
  66.     inIndex[0]++;
  67.     root.right = helperII(pre, in, preIndex, inIndex, target);
  68.     return root;
  69.   }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement