1. LCA in a binary tree 2. Find the medium of two sorted array 3. Find a number in a partially sorted matrix (the matrix is sorted column-wise and row-wise)
Sigiloso
q1. assumption: - we want LCA for just 2 node as input - the root of the binary tree is given as input - Node object contain left and right members Case 1: Each node are in different sections of the tree: LCA will be a node where both left and right subtrees contain input nodes) Case 2: A left or right path contains an input node, and current node is also an input node: LCA is the current node public Node LCASearch (Node root, Node n1, Node n2){ if (root == null || root == n1 || root = n2) return root; Node leftRes = LCASearch(root.left, n1, n2); Node rightRes = LCASearch(root.right, n1, n2); if(leftRes != null && rightRes!= null) { return root; } // return the one good result or the null result if(leftRes != null) return leftRes; else return rightRes; } q2. sln 1, o(n) use 2 pointer, or also o(n) space use 2 heap q3. if current matrix value > search value, check value to the left if current matrix value < search value, check value to the bottom row O(n) performance