LeetCode: Populating Next Right Pointers in Each Node 解题报告

Populating Next Right Pointers in Each Node Total
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.
You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).
For 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

SOLUTION 1

我们可以用递归处理左右子树.

 1 /* 试一下 recursion */
 2     public void connect(TreeLinkNode root) {
 3         if (root == null) {
 4             return;
 5         }
 6
 7         rec(root);
 8     }
 9
10     public void rec(TreeLinkNode root) {
11         if (root == null) {
12             return;
13         }
14
15         if (root.left != null) {
16             root.left.next = root.right;
17         }
18
19         if (root.right != null) {
20             root.right.next = root.next.left;
21         }
22
23         rec(root.left);
24         rec(root.right);
25     }

SOLUTION 2

用层次遍历也可以相当容易解出来,而且这种方法用在下一题一点不用变。

但是 这个解法不能符合题意。题目要求我们使用 constant extra space.

 1 /*
 2      * 使用level traversal来做。
 3      * */
 4     public void connect1(TreeLinkNode root) {
 5         if (root == null) {
 6             return;
 7         }
 8
 9         TreeLinkNode dummy = new TreeLinkNode(0);
10         Queue<TreeLinkNode> q = new LinkedList<TreeLinkNode>();
11         q.offer(root);
12         q.offer(dummy);
13
14         while (!q.isEmpty()) {
15             TreeLinkNode cur = q.poll();
16             if (cur == dummy) {
17                 if (!q.isEmpty()) {
18                     q.offer(dummy);
19                 }
20                 continue;
21             }
22
23             if (q.peek() == dummy) {
24                 cur.next = null;
25             } else {
26                 cur.next = q.peek();
27             }
28
29             if (cur.left != null) {
30                 q.offer(cur.left);
31             }
32
33             if (cur.right != null) {
34                 q.offer(cur.right);
35             }
36         }
37     }
时间: 2024-08-15 23:37:55

LeetCode: Populating Next Right Pointers in Each Node 解题报告的相关文章

[leetcode] Populating Next Right Pointers in Each Node

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, al

[leetcode]Populating Next Right Pointers in Each Node @ Python

原题地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node/ 题意: 1 / 2 3 / \ / 4 5 6 7变为: 1 -> NULL / 2 -> 3 -> NULL / \ / 4->5->6->7 -> NULL 解题思路:看到二叉树我们就想到需要使用递归的思路了.直接贴代码吧,思路不难. 代码: # Definition for a binary t

LeetCode——Populating Next Right Pointers in Each Node II

Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous solution still work? Note: You may only use constant extra space. For example, Given the following binary t

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

[题目] 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. Initiall

LeetCode: Populating Next Right Pointers in Each Node II [117]

[题目] Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous solution still work? Note: You may only use constant extra space. For example, Given the following bin

Leetcode | Populating Next Right Pointers in Each Node I &amp; II

Populating Next Right Pointers in Each Node I 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

leetcode - Populating Next Right Pointers in Each Node I&amp;II

Populating Next Right Pointers in Each Node I 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 nex

LeetCode: Populating Next Right Pointers in Each Node II 解题报告

Populating Next Right Pointers in Each Node IIFollow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous solution still work? Note: You may only use constant extra sp

[LeetCode] [LeetCode] Populating Next Right Pointers in Each Node II

Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous solution still work? Note: You may only use constant extra space. For example,Given the following binary tr