You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.
Initially, all next pointers are set to NULL.
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: []
题目大意
给定一棵完美二叉树(所有叶子节点在同一层,且每个父节点都有两个子节点),要求为每个节点的 next 指针赋值,使其指向同一层的右侧相邻节点。如果没有右侧相邻节点,则 next 指针设为 NULL。初始时,所有 next 指针均为 NULL。
例如:
输入完美二叉树 [1,2,3,4,5,6,7],处理后每个节点的 next 指针指向右侧节点,序列化输出为 [1,#,2,3,#,4,5,6,7,#](# 表示每层结束)。