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

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 tree,
         1
       /  \
      2    3
     / \    \
    4   5    7
After calling your function, the tree should look like:
         1 -> NULL
       /  \
      2 -> 3 -> NULL
     / \    \
    4-> 5 -> 7 -> NULL

SOLUTION 1

本题还是可以用Level Traversal 轻松解出,连代码都可以跟上一个题目一模一样。Populating Next Right Pointers in Each Node Total

但是不符合空间复杂度的要求:constant extra space.

 1 /**
 2  * Definition for binary tree with next pointer.
 3  * public class TreeLinkNode {
 4  *     int val;
 5  *     TreeLinkNode left, right, next;
 6  *     TreeLinkNode(int x) { val = x; }
 7  * }
 8  */
 9 public class Solution {
10     public void connect(TreeLinkNode root) {
11         if (root == null) {
12             return;
13         }
14
15         TreeLinkNode dummy = new TreeLinkNode(0);
16         Queue<TreeLinkNode> q = new LinkedList<TreeLinkNode>();
17
18         q.offer(root);
19         q.offer(dummy);
20
21         while(!q.isEmpty()) {
22             TreeLinkNode cur = q.poll();
23             if (cur == dummy) {
24                 if (!q.isEmpty()) {
25                     q.offer(dummy);
26                 }
27                 continue;
28             }
29
30             if (q.peek() == dummy) {
31                 cur.next = null;
32             } else {
33                 cur.next = q.peek();
34             }
35
36             if (cur.left != null) {
37                 q.offer(cur.left);
38             }
39
40             if (cur.right != null) {
41                 q.offer(cur.right);
42             }
43         }
44
45     }
46 }

SOLUTION 2

我们可以用递归解出。注意从右往左加next。否则的话 右边未建立,左边你没找不到next. 这个算符合constant extra space.

 1 public void connect(TreeLinkNode root) {
 2         if (root == null) {
 3             return;
 4         }
 5
 6         TreeLinkNode cur = root.next;
 7         TreeLinkNode next = null;
 8         // this is very important. should exit after found the next.
 9         while (cur != null && next == null) {
10             if (cur.left != null) {
11                 next = cur.left;
12             } else if (cur.right != null) {
13                 next = cur.right;
14             } else {
15                 cur = cur.next;
16             }
17         }
18
19         if (root.right != null) {
20             root.right.next = next;
21             next = root.right;
22         }
23
24         if (root.left != null) {
25             root.left.next = next;
26         }
27
28         // The order is very important. We should deal with right first!
29         connect(root.right);
30         connect(root.left);
31     }

时间: 2024-08-04 12:51:25

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

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 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] [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

[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

LeetCode - Populating Next Right Pointers in Each Node II 及其变形题

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

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

Populating Next Right Pointers in Each Node II leetcode java

题目: 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 bina

Leetcode 树 Populating Next Right Pointers in Each Node II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node II Total Accepted: 9695 Total Submissions: 32965 Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could