[LeetCode] 116. Populating Next Right Pointers in Each Node

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:

struct Node {
  int val;
  Node *left;
  Node *right;
  Node *next;
}

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.

Follow up:

  • You may only use constant extra space.
  • Recursive approach is fine, you may assume implicit stack space does not count as extra space for this problem.

 

Example 1:

[LeetCode] 116. Populating Next Right Pointers in Each Node_第1张图片

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.

Constraints:

  • The number of nodes in the given tree is less than 4096.
  • -1000 <= node.val <= 1000

填充每个节点的下一个右侧节点指针。题意是给一个完美二叉树,请你按照图示补足所有的next指针。

我这里给出两种思路,首先是BFS层序遍历。因为是一个完美二叉树所以可以这样做。版本二的时候这个方法则不行了。注意这道题在创建queue的时候,用了LinkedList而非Queue。

时间O(n)

空间O(n)

Java实现

 1 class Solution {
 2     public Node connect(Node root) {
 3         // corner case
 4         if (root == null) {
 5             return root;
 6         }
 7 
 8         // normal case
 9         LinkedList queue = new LinkedList<>();
10         queue.add(root);
11         while (!queue.isEmpty()) {
12             int size = queue.size();
13             Node temp = queue.get(0);
14             for (int i = 1; i < size; i++) {
15                 temp.next = queue.get(i);
16                 temp = queue.get(i);
17             }
18             for (int i = 0; i < size; i++) {
19                 temp = queue.remove();
20                 if (temp.left != null) {
21                     queue.add(temp.left);
22                 }
23                 if (temp.right != null) {
24                     queue.add(temp.right);
25                 }
26             }
27         }
28         return root;
29     }
30 }

 

题目的followup要求不使用额外空间,所以另外一种做法的思路如下

  • 从根节点开始遍历,如果当前节点没有左节点,说明已经遍历到最下面一层,可以直接退出了
  • 如果当前节点有左节点,则说明还没有到最下面一层,此时当前节点的左孩子的next指针是右孩子
  • 如果当前节点有next节点,说明当前节点不是根节点,此时还需要将当前节点的右孩子的next指针指向当前节点的next节点的左孩子(有点绕),并且在处理完这一步之后,移动到当前节点的next节点去继续处理
  • 当前层处理完之后,往左孩子走,去处理下一层节点

时间O(n)

空间O(1)

Java实现

 1 class Solution {
 2     public Node connect(Node root) {
 3         // corner case
 4         if (root == null) {
 5             return root;
 6         }
 7 
 8         // normal case
 9         Node pre = root;
10         while (pre.left != null) {
11             Node temp = pre;
12             while (temp != null) {
13                 temp.left.next = temp.right;
14                 // 如果temp不是root那一层
15                 if (temp.next != null) {
16                     temp.right.next = temp.next.left;
17                 }
18                 temp = temp.next;
19             }
20             pre = pre.left;
21         }
22         return root;
23     }
24 }

 

LeetCode 题目总结

你可能感兴趣的:([LeetCode] 116. Populating Next Right Pointers in Each Node)