Microsoft Interview Question

Find the next node in a binary tree from any node (implying successor to a node)

Interview Answers

Anonymous

Oct 17, 2014

Cracking the coding interview has most good questions including this one.

Anonymous

Oct 19, 2014

ode inorder_succ (Node root, Node p) { if (root == null || p == null) return null; if(p.right != null) { // if has right subtree // then succ is smallest in right subtree Node tmp = p.right; while(tmp.left != null){ tmp = tmp.left; } return tmp; } Node tmp = null; // Else we move down from root to find the node // Save the left parent. the last left parent is succ while(root!= null && root.data != p.data) { if(root.data >= p.data) { //moving left save the node tmp = root; root = root.left; } else { root = root.right } } return tmp; }

Anonymous

Oct 19, 2014

Node inorder_succ (Node root, Node p) { if (root == null || p == null) return null; if(p.right != null) { // if has right subtree // then succ is smallest in right subtree Node tmp = p.right; while(tmp.left != null){ tmp = tmp.left; } return tmp; } Node tmp = null; // Else we move down from root to find the node // Save the left parent. the last left parent is succ while(root!= null && root.data != p.data) { if(root.data >= p.data) { //moving left save the node tmp = root; root = root.left; } else { root = root.right } } return tmp; }