Skip to content

Commit c0f73fb

Browse files
committed
solutions: 1123 - Lowest Common Ancestor of Deepest Leaves (Medium)
1 parent 3aceb0b commit c0f73fb

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
---
2+
description: 'Author: @wkw | https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves'
3+
---
4+
5+
# 1123 - Lowest Common Ancestor of Deepest Leaves (Medium)
6+
7+
## Problem Link
8+
9+
https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves
10+
11+
## Problem Statement
12+
13+
Given the root of a binary tree, return the lowest common ancestor of its deepest leaves.
14+
15+
Recall that:
16+
17+
- The node of a binary tree is a leaf if and only if it has no children
18+
- The depth of the root of the tree is 0. if the depth of a node is d, the depth of each of its children is d + 1.
19+
- The lowest common ancestor of a set S of nodes, is the node A with the largest depth such that every node in S is in the subtree with root A.
20+
21+
**Example 1:**
22+
23+
**Input:** root = [3,5,1,6,2,0,8,null,null,7,4]
24+
25+
**Output:** [2,7,4]
26+
27+
**Explanation:**
28+
29+
We return the node with value 2, colored in yellow in the diagram. The nodes coloured in blue are the deepest leaf-nodes of the tree. Note that nodes 6, 0, and 8 are also leaf nodes, but the depth of them is 2, but the depth of nodes 7 and 4 is 3.
30+
31+
**Example 2:**
32+
33+
**Input:** root = [1]
34+
35+
**Output:** [1]
36+
37+
**Explanation:**
38+
39+
The root is the deepest node in the tree, and it's the lca of itself.
40+
41+
**Example 3:**
42+
43+
**Input:** root = [0,1,3,null,2]
44+
45+
**Output:** [2]
46+
47+
**Explanation:**
48+
49+
The deepest leaf node in the tree is 2, the lca of one node is itself.
50+
51+
**Constraints:**
52+
53+
- The number of nodes in the tree will be in the range $[1, 1000]$.
54+
- $0 <= Node.val <= 1000$
55+
- The values of the nodes in the tree are unique.
56+
57+
Note: This question is the same as 865: https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/
58+
59+
## Approach 1: DFS
60+
61+
We can write a DFS function to return a tuple $(depth, lca)$ where $depth$ is the depth of the deepest leaf in its subtree, while $lca$ is the lowest common ancestor (LCA) of all the deepest leaves in its subtree.
62+
63+
If the left subtree has a deeper leaf than the right one, then the LCA for the current node is the one returned by the left subtree. Similar logic for the opposite side. If the deepest leaves in both left & right subtrees are at the same depth, then the current node is the LCA.
64+
65+
Time complexity: $O(n)$
66+
67+
Space complexity: $O(n)$
68+
69+
<Tabs>
70+
<TabItem value="py" label="Python">
71+
<SolutionAuthor name="@wkw"/>
72+
73+
```py
74+
# Definition for a binary tree node.
75+
# class TreeNode:
76+
# def __init__(self, val=0, left=None, right=None):
77+
# self.val = val
78+
# self.left = left
79+
# self.right = right
80+
class Solution:
81+
def lcaDeepestLeaves(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
82+
def dfs(node):
83+
if not node: return (0, None) # (depth, lca)
84+
l, l_lca = dfs(node.left)
85+
r, r_lca = dfs(node.right)
86+
if l == r: return (l + 1, node)
87+
return (l + 1, l_lca) if l > r else (r + 1, r_lca)
88+
return dfs(root)[1]
89+
```
90+
91+
</TabItem>
92+
</Tabs>

0 commit comments

Comments
 (0)