-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTreePath.java
More file actions
153 lines (111 loc) · 3.72 KB
/
BinaryTreePath.java
File metadata and controls
153 lines (111 loc) · 3.72 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package Leetcode;
import java.lang.reflect.Array;
import java.util.*;
class TreeNode{
public TreeNode(int val) {
this.val = val;
}
int val;
TreeNode left;
TreeNode right;
}
public class BinaryTreePath {
/**
*
* 1
* / \
* 2 3
* \
* 5
*
* @param args
*/
public static void main(String[] args) {
TreeNode n1 = new TreeNode(1);
TreeNode n2 = new TreeNode(2);
TreeNode n3 = new TreeNode(3);
TreeNode n5 = new TreeNode(5);
n1.left = n2;
n1.right = n3;
n2.right = n5;
//System.out.println(binaryTreePaths(n1));
//System.out.println(binaryTreePaths1(n1).toString());
ArrayList<TreeNode> leafNodes = new ArrayList<TreeNode>();
printleafNode(n1,leafNodes);
List<String> result = new ArrayList<String>();
for (int i = 0; i < leafNodes.size() ; i++) {
result.add(preOrderTraversal(n1, leafNodes.get(i), ""));
}
System.out.println(result);
}
private static String preOrderTraversal(TreeNode root, TreeNode leafNode, String path) {
if (root == null){
return "";
}
path = path + root.val + "->";
if (root != leafNode){
preOrderTraversal(root.left, leafNode, path);
preOrderTraversal(root.right, leafNode, path);
}
return path;
}
public static List<String> binaryTreePaths(TreeNode root){
List<String> res = new ArrayList<String>();
Stack<TreeNode> stack = new Stack<TreeNode>();
HashSet<TreeNode> visitedNode = new HashSet<TreeNode>();
stack.push(root);
StringBuilder sb = new StringBuilder();
while (!stack.isEmpty()){
TreeNode curr = stack.pop();
visitedNode.add(curr);
sb.append(curr.val + "->");
if (curr.right !=null && !visitedNode.contains(curr.right)){
stack.push(curr.right);
visitedNode.add(curr.right);
}
if (curr.left !=null && !visitedNode.contains(curr.left)){
stack.push(curr.left);
visitedNode.add(curr.left);
}
if (curr.left ==null && curr.right == null){
res.add(sb.substring(0,sb.length()-2));
sb = new StringBuilder();
stack.push(root);
}
}
return res;
}
public static List<String> binaryTreePaths1(TreeNode root){
List<String> paths = new ArrayList<String>();
if (root == null) return paths;
searchBT(root, "", paths);
return paths;
}
private static void searchBT(TreeNode root, String s, List<String> paths) {
if (root.left == null && root.right ==null) {
paths.add(s + root.val);
}
if(root.left != null){
searchBT(root.left, s + root.val + "->", paths);
}
if (root.right != null){
searchBT(root.right, s + root.val + "->", paths);
}
}
private static List<TreeNode> printleafNode(TreeNode root, ArrayList<TreeNode> leafNodes){
if (root == null){
return null;
}
if (root.left == null && root.right == null){
// System.out.println(root.val);
leafNodes.add(root);
}
if (root.left != null){
printleafNode(root.left, leafNodes);
}
if (root.right !=null){
printleafNode(root.right, leafNodes);
}
return leafNodes;
}
}