給你一個 perfect binary tree ,把每個左邊節點的 next 對應到右邊節點上,沒有右邊節點就對應到 null ,最後把整個 tree 回傳。
perfect binary tree: 完美的二元樹,沒有缺失的節點
點我開啟限制與範例
限制:
The number of nodes in the tree is in the range [0, 212 - 1].
-1000 <= Node.val <= 1000
Example 1:
1 2 3
Input: root = [1,2,3,4,5,6,7] Output: [1,#,2,3,#,4,5,6,7,#] Explanation: Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
Example 2:
1 2
Input: root = [] Output: []
筆記
有兩個方法可以做這題:
假設
父節點 = root
左節點 = left
右節點 = right
1. DFS
left 很好想, left.next 就等於 right ,
但這題目的重點在 right , right 會跨樹,所以要透過 root 指定到旁邊那棵樹的 left
所以這個做法的重點是