Binary Search Tree - How to Succeed in Algorithms Interview


Binary Search Tree - How to Succeed in Algorithms Interview

How to Succeed in Algorithms Interview Series


Implementation Detail of Binary Search Tree

  • track and update [minVal, maxVal], or just min, max
  • in-order traverse
  • convert bst to linkedlist
  • track prev node
  • if (root.val > k1) go left otherwise go right
Range value [minVal, maxVal]
public void deleteEdge(TreeNode root) {
    if(root == null) return;
    root = dfs(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
}
private TreeNode dfs(TreeNode root, int left, int right) {
    if(root == null) return null;
    if(root.val <= left || root.val >= right) return null;
    root.left = dfs(root.left, left, root.val);
    root.right = dfs(root.right, root.val, right);
    return root;
}
Examples
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
    if (root == null || p == null || q == null) return null;
    if(root.val<Math.min(p.val,q.val)) return lowestCommonAncestor(root.right,p,q);
    if(root.val>Math.max(p.val,q.val)) return lowestCommonAncestor(root.left,p,q);
    return root;
}

In-order traverse

Node prev = null;
public Node treeToDoublyList(Node root) {
    if (root == null){
        return null;
    }
    Node dummy = new Node(0, null, null);
    prev = dummy;
    helper(root);
    //connect tail with head;
    prev.right = dummy.right;
    dummy.right.left = prev;
    return dummy.right;
}
private void helper(Node cur){
    if (cur == null){
        return;
    }
    helper(cur.left);
    prev.right = cur;
    cur.left = prev;
    prev = cur;
    helper(cur.right);
}

Postorder

  • left, right, root
  • scan backward from root to right then left
  • HARD Construct a Binary Search Tree from given postorder
    Node constructTreeUtil(int post[], int n)
    {
      Node root = new Node(post[n - 1]);
      Stack<Node> s = new Stack<>();
      s.push(root);
      for (int i = n - 2; i >= 0; --i) {
          Node x = new Node(post[i]);
          Node temp = null;
          while (!s.isEmpty() && post[i] < s.peek().data)  
              temp = s.pop();       
          // Make x as left child of temp    
          if (temp != null)  
              temp.left = x;       
          // Else make x as right of top       
          else
              s.peek().right = x;
          s.push(x);
      }
      return root;
    }

Labels

adsense (5) Algorithm (69) Algorithm Series (35) Android (7) ANT (6) bat (8) Big Data (7) Blogger (14) Bugs (6) Cache (5) Chrome (19) Code Example (29) Code Quality (7) Coding Skills (5) Database (7) Debug (16) Design (5) Dev Tips (63) Eclipse (32) Git (5) Google (33) Guava (7) How to (9) Http Client (8) IDE (7) Interview (88) J2EE (13) J2SE (49) Java (186) JavaScript (27) JSON (7) Learning code (9) Lesson Learned (6) Linux (26) Lucene-Solr (112) Mac (10) Maven (8) Network (9) Nutch2 (18) Performance (9) PowerShell (11) Problem Solving (11) Programmer Skills (6) regex (5) Scala (6) Security (9) Soft Skills (38) Spring (22) System Design (11) Testing (7) Text Mining (14) Tips (17) Tools (24) Troubleshooting (29) UIMA (9) Web Development (19) Windows (21) xml (5)