-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path114.flatten-binary-tree-to-linked-list.java
76 lines (62 loc) · 1.69 KB
/
114.flatten-binary-tree-to-linked-list.java
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.util.ArrayList;
import java.util.List;
import Parents.TreeNode;
/*
* @lc app=leetcode id=114 lang=java
*
* [114] Flatten Binary Tree to Linked List
*/
// @lc code=start
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution2 {
public static void main(String[] args) {
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(5);
root.left.left = new TreeNode(3);
root.left.right = new TreeNode(4);
root.right.right = new TreeNode(6);
flatten(root);
}
public static void flatten(TreeNode root) {
// 1
// 2 5
// 3 4 6
// 1 2 3 4 5 6
// If you notice carefully in the flattened tree, each node's right child points
// to the next node of a pre-order traversal.\
List<Integer> list = new ArrayList<>();
helper(root, list);
root = flat(root, list, 0);
}
private static TreeNode flat(TreeNode res, List<Integer> list, int i) {
if (i == list.size())
return res;
res = new TreeNode(list.get(i));
res.left = null;
res.right = flat(res.right, list, i + 1);
return res;
}
private static void helper(TreeNode root, List<Integer> list) {
if (root == null)
return;
list.add(root.val);
helper(root.left, list);
helper(root.right, list);
}
}
// @lc code=end