Description:
This question asked us to get 2 dimensional array which includes the value and level of tree.
"3" is the only value of first level of tree. 9, 20 are the second level. 15, 7 are the third level.
Each level is in the same list, we can see the different list in the output list.
Solution:
First, the solution used the queue method in collections library. "res" is what we are gonna return end of this question. We make a loop to run as long as the value exists on the queue.
The list name "level" is the list to append same level of the value.
In For loop, we are gonna figure out the child nodes. The variable "node" is the popped value from the queue.
If node exists, we load it on to the list named "level" to load on the name "res."
The queue now has the new values (child nodes), and then we load the parents to res.
'LeetCode ๐๏ธ > Tree' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
105. Construct Binary Tree from Preorder and Inorder Traversal (0) | 2023.06.02 |
---|---|
572. Subtree of Another Tree (0) | 2023.04.02 |
124. Binary Tree Maximum Path (0) | 2023.03.20 |
226. Invert Binary Tree (0) | 2023.03.20 |
100. Same Tree (1) | 2023.02.21 |