|
| 1 | +from collections import deque |
| 2 | +from typing import Optional, List |
| 3 | +from unittest import TestCase, main |
| 4 | + |
| 5 | + |
| 6 | +# Definition for a binary tree node. |
| 7 | +class TreeNode: |
| 8 | + def __init__(self, val=0, left=None, right=None): |
| 9 | + self.val = val |
| 10 | + self.left = left |
| 11 | + self.right = right |
| 12 | + |
| 13 | + |
| 14 | +class Solution: |
| 15 | + def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: |
| 16 | + return self.solve_dfs(root) |
| 17 | + |
| 18 | + """ |
| 19 | + Runtime: 0 ms (Beats 100.00%) |
| 20 | + Time Complexity: O(n) |
| 21 | + > 각 node를 bfs 방식으로 한 번 씩 조회하므로 O(n) |
| 22 | +
|
| 23 | + Memory: 17.42 (Beats 11.96%) |
| 24 | + Space Complexity: O(n) |
| 25 | + > 최악의 경우, deque에 최대 node의 갯수만큼 저장될 수 있으므로 O(n), upper bound |
| 26 | + """ |
| 27 | + def solve_bfs(self, root: Optional[TreeNode]) -> List[List[int]]: |
| 28 | + if not root: |
| 29 | + return [] |
| 30 | + |
| 31 | + dq = deque([root]) |
| 32 | + result = [] |
| 33 | + while dq: |
| 34 | + tmp_result = [] |
| 35 | + for _ in range(len(dq)): |
| 36 | + curr = dq.popleft() |
| 37 | + tmp_result.append(curr.val) |
| 38 | + |
| 39 | + if curr.left: |
| 40 | + dq.append(curr.left) |
| 41 | + if curr.right: |
| 42 | + dq.append(curr.right) |
| 43 | + |
| 44 | + result.append(tmp_result) |
| 45 | + |
| 46 | + return result |
| 47 | + |
| 48 | + """ |
| 49 | + Runtime: 1 ms (Beats 38.71%) |
| 50 | + Time Complexity: O(n) |
| 51 | + > 각 node를 dfs 방식으로 한 번 씩 조회하므로 O(n) |
| 52 | +
|
| 53 | + Memory: 17.49 (Beats 11.96%) |
| 54 | + Space Complexity: O(1) |
| 55 | + > 최악의 경우, list에 최대 node의 갯수만큼 저장될 수 있으므로 O(n), upper bound |
| 56 | + """ |
| 57 | + def solve_dfs(self, root: Optional[TreeNode]) -> List[List[int]]: |
| 58 | + if not root: |
| 59 | + return [] |
| 60 | + |
| 61 | + def dfs(node: TreeNode, depth: int): |
| 62 | + if len(result) <= depth: |
| 63 | + result.append([node.val]) |
| 64 | + else: |
| 65 | + result[depth].append(node.val) |
| 66 | + |
| 67 | + if node.left: |
| 68 | + dfs(node.left, depth + 1) |
| 69 | + if node.right: |
| 70 | + dfs(node.right, depth + 1) |
| 71 | + |
| 72 | + result = [] |
| 73 | + dfs(root, 0) |
| 74 | + |
| 75 | + return result |
| 76 | + |
| 77 | + |
| 78 | +class _LeetCodeTestCases(TestCase): |
| 79 | + |
| 80 | + def test_1(self): |
| 81 | + root = TreeNode(3) |
| 82 | + node1 = TreeNode(9) |
| 83 | + node2 = TreeNode(20) |
| 84 | + node3 = TreeNode(15) |
| 85 | + node4 = TreeNode(7) |
| 86 | + root.left = node1 |
| 87 | + root.right = node2 |
| 88 | + node2.left = node3 |
| 89 | + node2.right = node4 |
| 90 | + output = [[3], [9, 20], [15, 7]] |
| 91 | + self.assertEqual(Solution.levelOrder(Solution(), root), output) |
| 92 | + |
| 93 | + def test_2(self): |
| 94 | + root = TreeNode(1) |
| 95 | + node1 = TreeNode(2) |
| 96 | + node2 = TreeNode(3) |
| 97 | + node3 = TreeNode(4) |
| 98 | + node4 = TreeNode(5) |
| 99 | + root.left = node1 |
| 100 | + root.right = node2 |
| 101 | + node1.left = node3 |
| 102 | + node2.left = node4 |
| 103 | + output = [[1], [2, 3], [4, 5]] |
| 104 | + self.assertEqual(Solution.levelOrder(Solution(), root), output) |
| 105 | + |
| 106 | + |
| 107 | +if __name__ == '__main__': |
| 108 | + main() |
0 commit comments