Populating Next Right Pointers in Each Node II
Given a binary tree
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set toNULL
.
Initially, all next pointers are set toNULL
.
Note:
You may only use constant extra space.
Recursive approach is fine, implicit stack space does not count as extra space for this problem.
Example
Given the following binary tree,
After calling your function, the tree should look like:
Note
和之前不一样的是,这里不一定是perfect树了,所以会跨左右孩子,右连右这种情况
需要另外记一个head(这一层的第一个)和pre(前一个节点),根据这些进行连接
Code
Last updated