Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6ea2b17

Browse files
committed
connect
1 parentc4b1aec commit6ea2b17

File tree

1 file changed

+107
-1
lines changed

1 file changed

+107
-1
lines changed

‎tree/Connect2.java

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,19 @@
33
importjava.util.LinkedList;
44
importjava.util.Queue;
55

6+
/**
7+
* Definition for binary tree with next pointer.
8+
* public class TreeLinkNode {
9+
* int val;
10+
* TreeLinkNode left, right, next;
11+
* TreeLinkNode(int x) { val = x; }
12+
* }
13+
*/
614
publicclassConnect2 {
7-
publicvoidconnect(TreeLinkNoderoot) {
15+
/*
16+
Solution 1: space: O(N), the nodes of the last level.
17+
*/
18+
publicvoidconnect1(TreeLinkNoderoot) {
819
if (root ==null) {
920
return;
1021
}
@@ -38,6 +49,101 @@ public void connect(TreeLinkNode root) {
3849
q.offer(cur.right);
3950
}
4051
}
52+
}
53+
54+
/*
55+
Solution 2: recursion with O(h) space. H: the height of the tree.
56+
*/
57+
publicvoidconnect2(TreeLinkNoderoot) {
58+
if (root ==null) {
59+
return;
60+
}
4161

62+
TreeLinkNodecur =root.next;
63+
TreeLinkNodenext =null;
64+
// this is very important. should exit after found the next.
65+
while (cur !=null &&next ==null) {
66+
if (cur.left !=null) {
67+
next =cur.left;
68+
}elseif (cur.right !=null) {
69+
next =cur.right;
70+
}else {
71+
cur =cur.next;
72+
}
73+
}
74+
75+
if (root.right !=null) {
76+
root.right.next =next;
77+
next =root.right;
78+
}
79+
80+
if (root.left !=null) {
81+
root.left.next =next;
82+
}
83+
84+
// The order is very important. We should deal with right first!
85+
connect2(root.right);
86+
connect2(root.left);
87+
}
88+
89+
/*
90+
Solution 3: iterator with O(1) space.
91+
*/
92+
publicvoidconnect(TreeLinkNoderoot) {
93+
if (root ==null) {
94+
return;
95+
}
96+
97+
connIterator(root);
98+
}
99+
100+
/*
101+
This is a iterator version.
102+
*/
103+
publicvoidconnIterator(TreeLinkNoderoot) {
104+
TreeLinkNodeleftEnd =root;
105+
while (leftEnd !=null) {
106+
TreeLinkNodep =leftEnd;
107+
108+
// Connect all the nodes in the next level together.
109+
while (p !=null) {
110+
111+
// find the
112+
TreeLinkNodenext =findLeftEnd(p.next);
113+
114+
if (p.right !=null) {
115+
p.right.next =next;
116+
next =p.right;
117+
}
118+
119+
if (p.left !=null) {
120+
p.left.next =next;
121+
}
122+
123+
// continue to deal with the next point.
124+
p =p.next;
125+
}
126+
127+
// Find the left end of the NEXT LEVEL.
128+
leftEnd =findLeftEnd(leftEnd);
129+
}
130+
131+
}
132+
133+
// Find out the left end of the next level of Root TreeNode.
134+
publicTreeLinkNodefindLeftEnd(TreeLinkNoderoot) {
135+
while (root !=null) {
136+
if (root.left !=null) {
137+
returnroot.left;
138+
}
139+
140+
if (root.right !=null) {
141+
returnroot.right;
142+
}
143+
144+
root =root.next;
145+
}
146+
147+
returnnull;
42148
}
43149
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp