Skip to content

Commit c796695

Browse files
committed
populating next right pointer in each node
1 parent 3faa562 commit c796695

File tree

2 files changed

+86
-42
lines changed

2 files changed

+86
-42
lines changed

.idea/workspace.xml

Lines changed: 43 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.github.chen0040.leetcode.day18.medium;
2+
3+
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
8+
/**
9+
* Created by xschen on 13/8/2017.
10+
*
11+
* link: https://leetcode.com/problems/populating-next-right-pointers-in-each-node/description/
12+
*/
13+
public class PopulatingNextRightPointerInEachNode {
14+
public class TreeLinkNode {
15+
int val;
16+
TreeLinkNode left, right, next;
17+
TreeLinkNode(int x) { val = x; }
18+
}
19+
20+
public class Solution {
21+
Map<Integer, TreeLinkNode> levels = new HashMap<Integer, TreeLinkNode>();
22+
public void connect(TreeLinkNode root) {
23+
connect(root, 0);
24+
}
25+
26+
private void connect(TreeLinkNode x, int d) {
27+
if(x == null) {
28+
return;
29+
}
30+
31+
connect(x.left, d+1);
32+
33+
if(levels.containsKey(d)) {
34+
levels.get(d).next = x;
35+
levels.put(d, x);
36+
} else {
37+
levels.put(d, x);
38+
}
39+
40+
connect(x.right, d+1);
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)