Skip to content

Commit 5deb662

Browse files
committed
Populating Next Right Pointers in Each Node
1 parent e48c5cd commit 5deb662

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Author: illuz <iilluzen[at]gmail.com>
3+
* File: AC_simulation_n.cpp
4+
* Create Date: 2015-02-22 09:56:58
5+
* Descripton: Link children layer by layer, O(1) space
6+
* The code can accept in II version.
7+
*/
8+
9+
#include <bits/stdc++.h>
10+
11+
using namespace std;
12+
const int N = 0;
13+
14+
// Definition for binary tree with next pointer.
15+
struct TreeLinkNode {
16+
int val;
17+
TreeLinkNode *left, *right, *next;
18+
TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
19+
};
20+
21+
class Solution {
22+
public:
23+
void connect(TreeLinkNode *root) {
24+
TreeLinkNode header(0);
25+
header.next = root;
26+
TreeLinkNode *cur, *prev;
27+
while (header.next) {
28+
// current header's link is already populated
29+
// following will make a new children-link to header
30+
cur = header.next;
31+
prev = &header;
32+
header.next = nullptr;
33+
for (; cur; cur = cur->next) {
34+
if (cur->left != nullptr) {
35+
prev->next = cur->left;
36+
prev = prev->next;
37+
}
38+
if (cur->right != nullptr) {
39+
prev->next = cur->right;
40+
prev = prev->next;
41+
}
42+
}
43+
}
44+
}
45+
};
46+
47+
int main() {
48+
TreeLinkNode root(1);
49+
Solution s;
50+
s.connect(&root);
51+
return 0;
52+
}
53+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Author: illuz <iilluzen[at]gmail.com>
3+
* File: AC_simulation_n.java
4+
* Create Date: 2015-02-22 10:45:21
5+
* Descripton:
6+
*/
7+
8+
import java.util.Scanner;
9+
10+
public class Solution {
11+
12+
// Definition for binary tree with next pointer.
13+
public class TreeLinkNode {
14+
int val;
15+
TreeLinkNode left, right, next;
16+
TreeLinkNode(int x) { val = x; }
17+
}
18+
19+
public void connect(TreeLinkNode root) {
20+
TreeLinkNode header = new TreeLinkNode(0);
21+
header.next = root;
22+
TreeLinkNode cur, prev;
23+
while (header.next != null) {
24+
// current header's link is already populated
25+
// following will make a new children-link to header
26+
cur = header.next;
27+
prev = header;
28+
header.next = null;
29+
for (; cur != null; cur = cur.next) {
30+
if (cur.left != null) {
31+
prev.next = cur.left;
32+
prev = prev.next;
33+
}
34+
if (cur.right != null) {
35+
prev.next = cur.right;
36+
prev = prev.next;
37+
}
38+
}
39+
}
40+
}
41+
42+
// debug
43+
public static void main(String[] args) {
44+
Scanner cin = new Scanner(System.in);
45+
Solution s = new Solution();
46+
int[] input = {1, 2, 3, 1};
47+
System.out.println("no case");
48+
}
49+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
# Author: illuz <iilluzen[at]gmail.com>
4+
# File: AC_simulation_n.py
5+
# Create Date: 2015-02-22 10:32:38
6+
# Usage: AC_simulation_n.py
7+
# Descripton:
8+
9+
10+
# Definition for a binary tree node
11+
class TreeNode:
12+
def __init__(self, x):
13+
self.val = x
14+
self.left = None
15+
self.right = None
16+
self.next = None
17+
18+
class Solution:
19+
# @param root, a tree node
20+
# @return nothing
21+
def connect(self, root):
22+
header = TreeNode(0)
23+
header.next = root
24+
while header.next:
25+
cur = header.next
26+
prev = header
27+
header.next = None
28+
while cur:
29+
if cur.left:
30+
prev.next = cur.left
31+
prev = prev.next
32+
if cur.right:
33+
prev.next = cur.right
34+
prev = prev.next
35+
cur = cur.next
36+
37+
# debug
38+
root = TreeNode(0)
39+
s = Solution()
40+
s.connect(root)
41+

0 commit comments

Comments
 (0)