您的位置:首页 > Web前端 > Node.js

117. Populating Next Right Pointers in Each Node II

2017-04-25 10:03 267 查看
题目:

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


链接: http://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/

4/16/2017

BB电面准备

抄别人的答案,但是递归应该不是constant extra space

注意的问题

1. 第13-15要在root层一直找到root.next整个表中子树不为0的情况

2. 第17行找到子树层可以用于子树层next的节点

3. 第18,19行给子树层找到next

4. 最重要的是先找right再找left

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) return;
12         TreeLinkNode nextNode = root.next;
13         while (nextNode != null) {
14             if (nextNode.left == null && nextNode.right == null) nextNode = nextNode.next;
15             else break;
16         }
17         if (nextNode != null) nextNode = (nextNode.left == null? nextNode.right: nextNode.left);
18         if (root.right != null) root.right.next = nextNode;
19         if (root.left != null) root.left.next = (root.right == null? nextNode: root.right);
20         connect(root.right);
21         connect(root.left);
22     }
23 }


别人的答案,iterative方法

这种还是需要分层的方法,什么手段可以分层?可以多用几个指针,并且在next层每一个元素都同时有current层的元素指到。

https://discuss.leetcode.com/topic/8447/simple-solution-using-constant-space

1 public class Solution {
2     public void connect(TreeLinkNode root) {
3
4         while(root != null){
5             TreeLinkNode tempChild = new TreeLinkNode(0);
6             TreeLinkNode currentChild = tempChild;
7             while(root!=null){
8                 if(root.left != null) { currentChild.next = root.left; currentChild = currentChild.next;}
9                 if(root.right != null) { currentChild.next = root.right; currentChild = currentChild.next;}
10                 root = root.next;
11             }
12             root = tempChild.next;
13         }
14     }
15 }


https://discuss.leetcode.com/topic/1106/o-1-space-o-n-complexity-iterative-solution

1 public class Solution {
2
3     //based on level order traversal
4     public void connect(TreeLinkNode root) {
5
6         TreeLinkNode head = null; //head of the next level
7         TreeLinkNode prev = null; //the leading node on the next level
8         TreeLinkNode cur = root;  //current node of current level
9
10         while (cur != null) {
11
12             while (cur != null) { //iterate on the current level
13                 //left child
14                 if (cur.left != null) {
15                     if (prev != null) {
16                         prev.next = cur.left;
17                     } else {
18                         head = cur.left;
19                     }
20                     prev = cur.left;
21                 }
22                 //right child
23                 if (cur.right != null) {
24                     if (prev != null) {
25                         prev.next = cur.right;
26                     } else {
27                         head = cur.right;
28                     }
29                     prev = cur.right;
30                 }
31                 //move to next node
32                 cur = cur.next;
33             }
34
35             //move to next level
36             cur = head;
37             head = null;
38             prev = null;
39         }
40
41     }
42 }


更多讨论:

https://discuss.leetcode.com/category/125/populating-next-right-pointers-in-each-node-ii
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: