Skip to content

Commit 17ea9fd

Browse files
committed
Chapter 07 section 04 C++ codes updated. Java codes added.
1 parent 9214fd9 commit 17ea9fd

File tree

2 files changed

+61
-7
lines changed
  • 07-Binary-Tree-and-Recursion

2 files changed

+61
-7
lines changed

07-Binary-Tree-and-Recursion/Course Code (C++)/04-Binary-Tree-Paths/main.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
using namespace std;
66

7+
8+
/// 257. Binary Tree Paths
9+
/// https://leetcode.com/problems/binary-tree-paths/description/
10+
/// 时间复杂度: O(n), n为树中的节点个数
11+
/// 空间复杂度: O(h), h为树的高度
12+
713
/// Definition for a binary tree node.
814
struct TreeNode {
915
int val;
@@ -18,21 +24,21 @@ class Solution {
1824

1925
vector<string> res;
2026

21-
if( root == NULL )
27+
if(root == NULL)
2228
return res;
2329

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));
2632
return res;
2733
}
2834

2935
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]);
3238

3339
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]);
3642

3743
return res;
3844
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
}

0 commit comments

Comments
 (0)