File tree Expand file tree Collapse file tree 2 files changed +61
-7
lines changed
07-Binary-Tree-and-Recursion
Course Code (C++)/04-Binary-Tree-Paths
Course Code (Java)/04-Binary-Tree-Paths/src Expand file tree Collapse file tree 2 files changed +61
-7
lines changed Original file line number Diff line number Diff line change 4
4
5
5
using namespace std ;
6
6
7
+
8
+ // / 257. Binary Tree Paths
9
+ // / https://leetcode.com/problems/binary-tree-paths/description/
10
+ // / 时间复杂度: O(n), n为树中的节点个数
11
+ // / 空间复杂度: O(h), h为树的高度
12
+
7
13
// / Definition for a binary tree node.
8
14
struct TreeNode {
9
15
int val;
@@ -18,21 +24,21 @@ class Solution {
18
24
19
25
vector<string> res;
20
26
21
- if ( root == NULL )
27
+ if (root == NULL )
22
28
return res;
23
29
24
- if ( root->left == NULL && root->right == NULL ){
25
- res.push_back ( to_string (root->val ) );
30
+ if (root->left == NULL && root->right == NULL ){
31
+ res.push_back (to_string (root->val ));
26
32
return res;
27
33
}
28
34
29
35
vector<string> leftPaths = binaryTreePaths (root->left );
30
- for ( int i = 0 ; i < leftPaths.size () ; i ++ )
31
- res.push_back ( to_string (root->val ) + " ->" + leftPaths[i] );
36
+ for (int i = 0 ; i < leftPaths.size () ; i ++)
37
+ res.push_back (to_string (root->val ) + " ->" + leftPaths[i]);
32
38
33
39
vector<string> rightPaths = binaryTreePaths (root->right );
34
- for ( int i = 0 ; i < rightPaths.size () ; i ++ )
35
- res.push_back ( to_string (root->val ) + " ->" + rightPaths[i] );
40
+ for (int i = 0 ; i < rightPaths.size () ; i ++)
41
+ res.push_back (to_string (root->val ) + " ->" + rightPaths[i]);
36
42
37
43
return res;
38
44
}
Original file line number Diff line number Diff line change
1
+ import java .util .List ;
2
+ import java .util .ArrayList ;
3
+
4
+ /// 257. Binary Tree Paths
5
+ /// https://leetcode.com/problems/binary-tree-paths/description/
6
+ /// 时间复杂度: O(n), n为树中的节点个数
7
+ /// 空间复杂度: O(h), h为树的高度
8
+ public class Solution {
9
+
10
+ // Definition for a binary tree node.
11
+ public class TreeNode {
12
+ int val ;
13
+ TreeNode left ;
14
+ TreeNode right ;
15
+ TreeNode (int x ) { val = x ; }
16
+ }
17
+
18
+ public List <String > binaryTreePaths (TreeNode root ) {
19
+
20
+ ArrayList <String > res = new ArrayList <String >();
21
+
22
+ if (root == null )
23
+ return res ;
24
+
25
+ if (root .left == null && root .right == null ){
26
+ res .add (Integer .toString (root .val ));
27
+ return res ;
28
+ }
29
+
30
+ List <String > leftPaths = binaryTreePaths (root .left );
31
+ for (String s : leftPaths ){
32
+ StringBuilder sb = new StringBuilder (Integer .toString (root .val ));
33
+ sb .append ("->" );
34
+ sb .append (s );
35
+ res .add (sb .toString ());
36
+ }
37
+
38
+ List <String > rightPaths = binaryTreePaths (root .right );
39
+ for (String s : rightPaths ) {
40
+ StringBuilder sb = new StringBuilder (Integer .toString (root .val ));
41
+ sb .append ("->" );
42
+ sb .append (s );
43
+ res .add (sb .toString ());
44
+ }
45
+
46
+ return res ;
47
+ }
48
+ }
You can’t perform that action at this time.
0 commit comments