-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbinary_tree_paths.dart
120 lines (93 loc) · 2.4 KB
/
binary_tree_paths.dart
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
/*
-* Binary Tree Paths *-
Given the root of a binary tree, return all root-to-leaf paths in any order.
A leaf is a node with no children.
Example 1:
Input: root = [1,2,3,null,5]
Output: ["1->2->5","1->3"]
Example 2:
Input: root = [1]
Output: ["1"]
Constraints:
The number of nodes in the tree is in the range [1, 100].
-100 <= Node.val <= 100
*/
// Definition for a binary tree node.
class TreeNode {
int val;
TreeNode? left;
TreeNode? right;
TreeNode([this.val = 0, this.left, this.right]);
}
/*
*/
class A {
// Runtime: 466 ms, faster than 50.00% of Dart online submissions for Binary Tree Paths.
// Memory Usage: 142.6 MB, less than 50.00% of Dart online submissions for Binary Tree Paths.
List<String> binaryTreePaths(TreeNode? root) {
List<String> res = [];
if (root == null) return res;
dfs(root, res, "");
return res;
}
void dfs(TreeNode? root, List ls, String str) {
if (root == null) return;
str += (str.length == 0 ? "" : "->") + root.val.toString();
if (root.left == null && root.right == null) {
ls.add(str);
return;
}
dfs(root.left, ls, str);
dfs(root.right, ls, str);
}
}
class B {
// wrong
List binaryTreePaths(TreeNode? root) {
List<String> res = [];
StringBuffer sb = StringBuffer();
if (root == null) return res;
dfs(root, res, sb);
return res;
}
void dfs(TreeNode? root, List ls, StringBuffer str) {
if (root == null) return;
str.write((str.length == 0 ? "" : "->") + root.val.toString());
if (root.left == null && root.right == null) {
ls.add(str.toString());
return;
}
dfs(root.left, ls, str);
str.length;
dfs(root.right, ls, str);
str.length;
}
}
class C {
List<String> binaryTreePaths(TreeNode? root) {
List<String> res = [];
if (root == null) {
return res;
}
StringBuffer sb = StringBuffer();
dfs(root, res, sb);
return res;
}
void dfs(TreeNode root, List<String> res, StringBuffer sb) {
if (root.left == null && root.right == null) {
sb.write("" + root.val.toString());
res.add(sb.toString());
return;
}
if (root.left != null) {
String prev = sb.toString();
sb.write(root.val.toString() + "->");
dfs(root.left!, res, sb);
sb = StringBuffer([prev]);
}
if (root.right != null) {
sb.write(root.val.toString() + "->");
dfs(root.right!, res, sb);
}
}
}