-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinodeLcci1712.java
More file actions
54 lines (48 loc) · 1.08 KB
/
BinodeLcci1712.java
File metadata and controls
54 lines (48 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package medium;
import java.util.LinkedList;
public class BinodeLcci1712
{
public TreeNode convertBiNode(TreeNode root)
{
LinkedList<TreeNode> link = new LinkedList<TreeNode>();
midOrder(root, link);
TreeNode first = null;
TreeNode tmp = null;
for (TreeNode treeNode : link) {
treeNode.left = null;
treeNode.right = null;
if (first == null) {
first = treeNode;
tmp = treeNode;
}
else {
tmp.right = treeNode;
tmp = treeNode;
}
}
return first;
}
public static void midOrder(TreeNode root, LinkedList<TreeNode> link)
{
if (root == null) {
return;
}
if (root.left != null) {
midOrder(root.left, link);
}
link.add(root);
if (root.right != null) {
midOrder(root.right, link);
}
}
}
class TreeNode
{
int val;
TreeNode left;
TreeNode right;
TreeNode(int x)
{
val = x;
}
}