Problem:
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
For example:
Given binary tree [3,9,20,null,null,15,7],
-Summary-
Solve by BFS (Breadth-First Search) Algorithm using queue
1. Create a queue and keep adding the value if left or right subtree exists
2. While adding the value to the queue, we pop and add the value to the 'res' list.
3. Once all nodes visited by each level, it will return the correct answer.
BFS 가 아닌 DFS로도 풀수있는 방법이 discussion에 있었다.
leetcode.com/problems/binary-tree-level-order-traversal/discuss/33731/Python-short-dfs-solution
모든 문제에 대한 저작권은 LeetCode 회사에 있습니다. [Copyright © 2020 LeetCode]
'LeetCode > Top Interview Q. - Easy' 카테고리의 다른 글
LeetCode. Merge Sorted Array [Sorting and Searching] (0) | 2020.06.08 |
---|---|
LeetCode. Convert Sorted Array to Binary Search Tree [Trees] (0) | 2020.06.07 |
LeetCode. Symmetric Tree [Trees] (0) | 2020.06.07 |
LeetCode. Validate Binary Search Tree [Trees] (0) | 2020.06.06 |
LeetCode. Maximum Depth of Binary Tree [Trees] (0) | 2020.06.05 |
댓글