[Java]LeetCode116. 填充同一层的兄弟节点 | Populating Next Right Pointers in Each Node

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9953049.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

Given a binary tree

struct TreeLinkNode {
  TreeLinkNode *left;
  TreeLinkNode *right;
  TreeLinkNode *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.

Note:

  • You may only use constant extra space.
  • Recursive approach is fine, implicit stack space does not count as extra space for this problem.
  • You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).

Example:

Given the following perfect binary tree,

     1
   /  
  2    3
 /   / 
4  5  6  7

After calling your function, the tree should look like:

     1 -> NULL
   /  
  2 -> 3 -> NULL
 /   / 
4->5->6->7 -> NULL

给定一个二叉树

struct TreeLinkNode {
  TreeLinkNode *left;
  TreeLinkNode *right;
  TreeLinkNode *next;
}

填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL

初始状态下,所有 next 指针都被设置为 NULL

说明:

  • 你只能使用额外常数空间。
  • 使用递归解题也符合要求,本题中递归程序占用的栈空间不算做额外的空间复杂度。

示例:

给定二叉树,

     1
   /  
  2    3
 /     
4   5    7

调用你的函数后,该二叉树变为:

     1 -> NULL
   /  
  2 -> 3 -> NULL
 /     
4-> 5 -> 7 -> NULL

0ms
 1 /*
 2 // Definition for a Node.
 3 class Node {
 4     public int val;
 5     public Node left;
 6     public Node right;
 7     public Node next;
 8 
 9     public Node() {}
10 
11     public Node(int _val,Node _left,Node _right,Node _next) {
12         val = _val;
13         left = _left;
14         right = _right;
15         next = _next;
16     }
17 };
18 */
19 class Solution {
20     public Node connect(Node root) {
21         Node levelStart = root;
22         while (levelStart != null){
23             Node cur = levelStart;
24             while (cur != null){
25                 if (cur.left != null){
26                     cur.left.next = cur.right;
27                 }
28                 if (cur.right != null && cur.next != null){
29                     cur.right.next = cur.next.left;
30                 }
31                 cur = cur.next;
32             }
33             levelStart = levelStart.left;
34         }
35         return root;        
36     }
37 }

1ms

 1 class Solution {
 2     public Node connect(Node root) {
 3 
 4         if (root == null)
 5             return root;
 6 
 7         Queue<Node> queue = new LinkedList<>();
 8         queue.offer(root);
 9 
10         while (!queue.isEmpty()) {
11 
12             List<Integer> currentLayer = new ArrayList<>();
13             int layerSize = queue.size();
14             for (int i = 0; i < layerSize; i++) {
15 
16                 Node currentNode = queue.remove();
17                 currentLayer.add(currentNode.val);
18 
19                 if (currentNode.left != null)
20                     queue.add(currentNode.left);
21                 if (currentNode.right != null)
22                     queue.add(currentNode.right);
23                 
24                 if( i < layerSize-1 ) {
25                     currentNode.next = queue.peek();
26                 } else {
27                     currentNode.next = null;
28                 }
29             }
30         }
31         return root;
32     }
33 }

 1 class Solution {
 2     public Node connect(Node root) {
 3         if (root == null) return null;
 4         
 5         Queue<Node> queue = new ArrayDeque<>();
 6         
 7         Node current;
 8         int remainingNodesAtCurrentLevel = 1;
 9         int nbNodesPreviousLevel = 1;
10         queue.offer(root);
11         while (!queue.isEmpty()) {
12             current = queue.poll();
13             remainingNodesAtCurrentLevel--;
14             
15             if (current.left != null) { //because it's perfect
16                 queue.offer(current.left);
17                 queue.offer(current.right);
18             }
19             
20             if (remainingNodesAtCurrentLevel == 0) {
21                 current.next = null;
22                 nbNodesPreviousLevel = nbNodesPreviousLevel * 2;
23                 remainingNodesAtCurrentLevel = nbNodesPreviousLevel;
24                 
25                 continue;
26             }
27             
28             System.out.println(current.val + " -- " + queue.peek().val);
29             
30             current.next = queue.peek();
31         }
32         
33         return root;
34     }
35 }
原文地址:https://www.cnblogs.com/strengthen/p/9953049.html