sweet1cris

Untitled

Oct 22nd, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.76 KB | None | 0 0
  1.  
  2.     // idxMap is the hash_table that maps each node in the
  3.     // in-order array to its index in in-order array. e.g.
  4.     // <key = 10, value = 3>
  5.     // Recursion function returns the sub-tree root.
  6.     public TreeNode construct(int[] in, int inLeft, int inRight, int[] pre, int preLeft, int preRight,
  7.             Map<Integer, Integer> idxMap) {
  8.         if (inLeft > inRight) {
  9.             return null;// base case
  10.         }
  11.  
  12.         TreeNode root = new TreeNode(pre[preLeft]);
  13.         int leftSize = idxMap.get(root.key) - inLeft; // calculate left-subtree
  14.                                                         // size
  15.         root.left = construct(in, inLeft, inLeft + leftSize - 1, pre, preLeft + 1, preLeft + leftSize, idxMap);
  16.         root.right = construct(in, inLeft + leftSize + 1, inRight, pre, preLeft + leftSize + 1, preRight, idxMap);
  17.         return root;
  18.     }
Advertisement
Add Comment
Please, Sign In to add comment