-
Notifications
You must be signed in to change notification settings - Fork 17
/
find-leaves-of-binary-tree.py
43 lines (32 loc) · 1.14 KB
/
find-leaves-of-binary-tree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import heapq
from typing import List, Tuple
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def findLeaves(self, root: TreeNode) -> List[List[int]]:
if not root:
return []
heap: List[Tuple[int, int]] = []
def dfs(node: TreeNode, heap: List[Tuple[int, int]]):
depth = 0
if node.left:
depth = max(depth, dfs(node.left, heap))
if node.right:
depth = max(depth, dfs(node.right, heap))
# Can be solved via bucket sort also, which will give use O(n) time
# instead of O(nlogn), but I'm too tired to implement it now.
heapq.heappush(heap, (depth, node.val))
return depth + 1
dfs(root, heap)
depth = None
result = [[]]
while heap:
depth_cur, node = heapq.heappop(heap)
if depth is not None and depth != depth_cur:
result.append([])
depth = depth_cur
result[-1].append(node)
return result