1. problem
- level order traversal of its nodes’ values. e.g.
Input: root = [3,9,20,null,null,15,7]
Output: [[3],[9,20],[15,7]] - like BFS
2. init
# Definition for a binary tree node. |
3. thought
- iterator
- init stack root
- open it and add val into [], result append it.
- add it’s child into stack. left first.
- iterate each item in stack.
- recursive
- global list, clean in init
- add [] in lsit when first time traverse to this depth.
- add val in depth list
4. trouble
- iterator
- keep same level node in stack
- one func
- recursive
- fill node’s val in certain position
- two func, one global list
5.final solution
class Solution: |
recursive way.
# https://leetcode.com/problems/binary-tree-level-order-traversal/solutions/33468 |