Lowest Common Ancestor in a BST and a Binary Tree
Updated
Problem statement
You are given the root of a binary tree, two different node values p and q, and a boolean isBst. Return the value of the lowest common ancestor of the node with value p and the node with value q.
A node is a common ancestor of the two nodes if both of them are in its subtree. A node counts as part of its own subtree, so one of the two nodes can be the answer. The lowest common ancestor is the common ancestor that is farthest from the root.
The question has two parts, and isBst tells you which one a test belongs to:
- Part (a),
isBst = true: the tree is a binary search tree. For every node, all values in its left subtree are smaller than the node's value, and all values in its right subtree are larger. - Part (b),
isBst = false: the same question for a general binary tree. The values need not follow any order, so they cannot tell you which way to go.
The tree is given in level order, with null for a missing child.
Function
lowestCommonAncestor(root: TreeNode, p: int, q: int, isBst: boolean) → intExamples
Example 1
root = [8,3,12,1,6,10,15,null,null,4,7]p = 1q = 7isBst = truereturn = 3Both 1 and 7 are smaller than the root 8, so both are in its left subtree. At 3 they split: 1 is on the left and 7 is on the right. So the answer is 3.
Example 2
root = [8,3,12,1,6,10,15,null,null,4,7]p = 12q = 10isBst = truereturn = 1210 is the left child of 12. A node counts as part of its own subtree, so 12 itself is the lowest common ancestor.
Example 3
root = [5,9,2,4,7,null,1,null,null,3,8]p = 3q = 4isBst = falsereturn = 9The values follow no order here. 4 is the left child of 9, and 3 is under 9's right child 7. The lowest node with both of them below it is 9.
Example 4
root = [5,9,2,4,7,null,1,null,null,3,8]p = 8q = 1isBst = falsereturn = 58 is in the root's left subtree and 1 is in its right subtree, so the answer is the root 5.
Constraints
- The number of nodes in the tree is in the range
[2, 10^4]. -10^9 <= Node.val <= 10^9- All
Node.valare unique. p != q, and bothpandqare values of nodes in the tree.- If
isBstistrue, the whole tree is a binary search tree as described above.
More GreyOrange interview questions
Similar questions at other companies
- Add All Larger Values in a BSTAdobe · PHONE SCREENAdobe interview questions
- Inorder Predecessor in a BSTNutanix · PHONE SCREENNutanix interview questions
- Shortest Path Between BST NodesGoogle · ONSITE INTERVIEWGoogle interview questions
- Validate Binary Search TreeTokopedia · ONSITE INTERVIEWTokopedia interview questions
- Check the Order of a Binary Search TreeCapital One · ONSITE INTERVIEWCapital One interview questions
- N-ary Index-Path Encoder and DecoderBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- House Robber IIIOkta · PHONE SCREENOkta interview questions
- Render a Task Tree from CSV RowsStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- Validate Binary Search TreeKotak Mahindra Bank · PHONE SCREENKotak Mahindra Bank interview questions
- House Robber IIIGoldman Sachs · PHONE SCREENGoldman Sachs interview questions