|
2 | 2 |
|
3 | 3 | import com.fishercoder.common.classes.TreeLinkNode;
|
4 | 4 |
|
5 |
| -/** |
6 |
| - * 116. Populating Next Right Pointers in Each Node |
7 |
| -
|
8 |
| - Given a binary tree |
9 |
| -
|
10 |
| - struct TreeLinkNode { |
11 |
| - TreeLinkNode *left; |
12 |
| - TreeLinkNode *right; |
13 |
| - TreeLinkNode *next; |
14 |
| - } |
15 |
| - 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. |
16 |
| -
|
17 |
| - Initially, all next pointers are set to NULL. |
18 |
| -
|
19 |
| - Note: |
20 |
| -
|
21 |
| - You may only use constant extra space. |
22 |
| - You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children). |
23 |
| - For example, |
24 |
| - Given the following perfect binary tree, |
25 |
| - 1 |
26 |
| - / \ |
27 |
| - 2 3 |
28 |
| - / \ / \ |
29 |
| - 4 5 6 7 |
30 |
| -
|
31 |
| - After calling your function, the tree should look like: |
32 |
| - 1 -> NULL |
33 |
| - / \ |
34 |
| - 2 -> 3 -> NULL |
35 |
| - / \ / \ |
36 |
| - 4->5->6->7 -> NULL |
37 |
| - */ |
38 |
| - |
39 | 5 | public class _116 {
|
40 |
| - public static class Solution1 { |
41 |
| - /** |
42 |
| - * credit: https://discuss.leetcode.com/topic/1106/o-1-space-o-n-complexity-iterative-solution |
43 |
| - * based on level order traversal |
44 |
| - */ |
45 |
| - public void connect(TreeLinkNode root) { |
46 |
| - |
47 |
| - TreeLinkNode head = null; //head of the next level |
48 |
| - TreeLinkNode prev = null; //the leading node on the next level |
49 |
| - TreeLinkNode curr = root; //current node of current level |
50 |
| - |
51 |
| - while (curr != null) { |
52 |
| - while (curr != null) { //iterate on the current level |
53 |
| - //left child |
54 |
| - if (curr.left != null) { |
55 |
| - if (prev != null) { |
56 |
| - prev.next = curr.left; |
57 |
| - } else { |
58 |
| - head = curr.left; |
59 |
| - } |
60 |
| - prev = curr.left; |
61 |
| - } |
62 |
| - //right child |
63 |
| - if (curr.right != null) { |
64 |
| - if (prev != null) { |
65 |
| - prev.next = curr.right; |
66 |
| - } else { |
67 |
| - head = curr.right; |
| 6 | + public static class Solution1 { |
| 7 | + /** |
| 8 | + * credit: https://discuss.leetcode.com/topic/1106/o-1-space-o-n-complexity-iterative-solution |
| 9 | + * based on level order traversal |
| 10 | + */ |
| 11 | + public void connect(TreeLinkNode root) { |
| 12 | + |
| 13 | + TreeLinkNode head = null; //head of the next level |
| 14 | + TreeLinkNode prev = null; //the leading node on the next level |
| 15 | + TreeLinkNode curr = root; //current node of current level |
| 16 | + |
| 17 | + while (curr != null) { |
| 18 | + while (curr != null) { //iterate on the current level |
| 19 | + //left child |
| 20 | + if (curr.left != null) { |
| 21 | + if (prev != null) { |
| 22 | + prev.next = curr.left; |
| 23 | + } else { |
| 24 | + head = curr.left; |
| 25 | + } |
| 26 | + prev = curr.left; |
| 27 | + } |
| 28 | + //right child |
| 29 | + if (curr.right != null) { |
| 30 | + if (prev != null) { |
| 31 | + prev.next = curr.right; |
| 32 | + } else { |
| 33 | + head = curr.right; |
| 34 | + } |
| 35 | + prev = curr.right; |
| 36 | + } |
| 37 | + //move to next node |
| 38 | + curr = curr.next; |
| 39 | + } |
| 40 | + //move to next level |
| 41 | + curr = head; |
| 42 | + head = null; |
| 43 | + prev = null; |
68 | 44 | }
|
69 |
| - prev = curr.right; |
70 |
| - } |
71 |
| - //move to next node |
72 |
| - curr = curr.next; |
73 | 45 | }
|
74 |
| - //move to next level |
75 |
| - curr = head; |
76 |
| - head = null; |
77 |
| - prev = null; |
78 |
| - } |
79 | 46 | }
|
80 |
| - } |
81 | 47 | }
|
0 commit comments