-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinaryTreePaths.java
More file actions
142 lines (107 loc) · 2.89 KB
/
BinaryTreePaths.java
File metadata and controls
142 lines (107 loc) · 2.89 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
import java.util.ArrayList;
import java.util.List;
/**
*
* 257 Binary Tree Paths 20.5% Easy
* @author cicean
*
* LeetCode: Binary Tree Paths
AUG 16 2015
Given a binary tree, return all root-to-leaf paths.
For example
Given the following binary tree:
1
1
/ \
2 3
\
5
All root-to-leaf paths are: ["1->2->5", "1->3"]
*/
public class BinaryTreePaths {
//时间O(n) 空间栈O(n logn)
//时间 O(b^(h+1)-1) 空间 O(h) 递归栈空间 对于二叉树b=2
public class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> res = new ArrayList<>();
if (root == null) return res;
dfs(root, res, "");
return res;
}
private void dfs(TreeNode root, List<String> res, String path) {
if (root.left == null && root.right == null) {
res.add(path + root.val);
return;
}
path = path + root.val + "->";
if (root.left != null) dfs(root.left, res, path);
if (root.right != null) dfs(root.right, res, path);
}
}
public List<String> binaryTreePaths(TreeNode root) {
List<String> ret = new ArrayList<String>();
if (root == null) {
return ret;
}
dfs(root, new StringBuilder(), ret);
return ret;
}
public void dfs(TreeNode root, StringBuilder sb, List<String> ret) {
if (root.left == null && root.right == null) {
sb.append(root.val);
ret.add(sb.toString());
return;
}
sb.append(root.val);
sb.append("->");
if (root.left != null) {
dfs(root.left, new StringBuilder(sb), ret);
}
if (root.right != null) {
dfs(root.right, new StringBuilder(sb), ret);
}
}
//My java non-recursion solution using stack and wrapper
private class Wrapper {
private TreeNode node;
private String path;
public Wrapper(TreeNode node, String path) {
this.node = node;
this.path = path;
}
}
// non-recursion-version
public List<String> binaryTreePaths(TreeNode root) {
List<String> res = new LinkedList<>();
if (root == null) {
return res;
}
Stack<Wrapper> stack = new Stack<>();
stack.add(new Wrapper(root, ""+root.val));
while(!stack.isEmpty()){
Wrapper wrapper = stack.pop();
if (wrapper.node.left == null && wrapper.node.right == null) {
res.add(wrapper.path);
}
if (wrapper.node.left != null) {
stack.add(new Wrapper(wrapper.node.left, wrapper.path + "->" + wrapper.node.left.val));
}
if (wrapper.node.right != null) {
stack.add(new Wrapper(wrapper.node.right, wrapper.path + "->" + wrapper.node.right.val));
}
}
return res;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeNode root = new TreeNode(1);
TreeNode lf1 = new TreeNode(2);
TreeNode lf2 = new TreeNode(3);
TreeNode lf3 = new TreeNode(5);
root.left = lf1;
root.right = lf2;
lf1.right = lf3;
BinaryTreePaths slt = new BinaryTreePaths();
System.out.println(slt.binaryTreePaths(root));
}
}