Skip to content

Commit a54ed54

Browse files
committed
feat: add python and java solutions to leetcode problem: No.0257
See https://leetcode-cn.com/problems/binary-tree-paths/
1 parent b0b7036 commit a54ed54

File tree

6 files changed

+159
-17
lines changed

6 files changed

+159
-17
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
1. [二叉树的层次遍历 II](/solution/0100-0199/0107.Binary%20Tree%20Level%20Order%20Traversal%20II/README.md)
9393
1. [二叉树的最大深度](/solution/0100-0199/0104.Maximum%20Depth%20of%20Binary%20Tree/README.md)
9494
1. [二叉树的最小深度](/solution/0100-0199/0111.Minimum%20Depth%20of%20Binary%20Tree/README.md)
95+
1. [二叉树的所有路径](/solution/0200-0299/0257.Binary%20Tree%20Paths/README.md)
9596
1. [路径总和](/solution/0100-0199/0112.Path%20Sum/README.md)
9697
1. [路径总和 II](/solution/0100-0199/0113.Path%20Sum%20II/README.md)
9798
1. [从前序与中序遍历序列构造二叉树](/solution/0100-0199/0105.Construct%20Binary%20Tree%20from%20Preorder%20and%20Inorder%20Traversal/README.md)

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
8989
1. [Binary Tree Level Order Traversal II](/solution/0100-0199/0107.Binary%20Tree%20Level%20Order%20Traversal%20II/README_EN.md)
9090
1. [Maximum Depth of Binary Tree](/solution/0100-0199/0104.Maximum%20Depth%20of%20Binary%20Tree/README_EN.md)
9191
1. [Minimum Depth of Binary Tree](/solution/0100-0199/0111.Minimum%20Depth%20of%20Binary%20Tree/README_EN.md)
92+
1. [Binary Tree Paths](/solution/0200-0299/0257.Binary%20Tree%20Paths/README_EN.md)
9293
1. [Path Sum](/solution/0100-0199/0112.Path%20Sum/README_EN.md)
9394
1. [Path Sum II](/solution/0100-0199/0113.Path%20Sum%20II/README_EN.md)
9495
1. [Construct Binary Tree from Preorder and Inorder Traversal](/solution/0100-0199/0105.Construct%20Binary%20Tree%20from%20Preorder%20and%20Inorder%20Traversal/README_EN.md)

solution/0200-0299/0257.Binary Tree Paths/README.md

+56-2
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,76 @@
2727

2828
<!-- 这里可写通用的实现逻辑 -->
2929

30+
深度优先搜索+路径记录。
31+
3032
<!-- tabs:start -->
3133

3234
### **Python3**
3335

3436
<!-- 这里可写当前语言的特殊实现逻辑 -->
3537

3638
```python
37-
39+
# Definition for a binary tree node.
40+
# class TreeNode:
41+
# def __init__(self, x):
42+
# self.val = x
43+
# self.left = None
44+
# self.right = None
45+
46+
class Solution:
47+
def binaryTreePaths(self, root: TreeNode) -> List[str]:
48+
def dfs(root):
49+
if root is None:
50+
return
51+
path.append(str(root.val))
52+
if root.left is None and root.right is None:
53+
res.append("->".join(path))
54+
dfs(root.left)
55+
dfs(root.right)
56+
path.pop()
57+
res = []
58+
path = []
59+
dfs(root)
60+
return res
3861
```
3962

4063
### **Java**
4164

4265
<!-- 这里可写当前语言的特殊实现逻辑 -->
4366

4467
```java
45-
68+
/**
69+
* Definition for a binary tree node.
70+
* public class TreeNode {
71+
* int val;
72+
* TreeNode left;
73+
* TreeNode right;
74+
* TreeNode(int x) { val = x; }
75+
* }
76+
*/
77+
class Solution {
78+
private List<String> res;
79+
private List<String> path;
80+
81+
public List<String> binaryTreePaths(TreeNode root) {
82+
if (root == null) return Collections.emptyList();
83+
res = new ArrayList<>();
84+
path = new ArrayList<>();
85+
dfs(root);
86+
return res;
87+
}
88+
89+
private void dfs(TreeNode root) {
90+
if (root == null) return;
91+
path.add(String.valueOf(root.val));
92+
if (root.left == null && root.right == null) {
93+
res.add(String.join("->", path));
94+
}
95+
dfs(root.left);
96+
dfs(root.right);
97+
path.remove(path.size() - 1);
98+
}
99+
}
46100
```
47101

48102
### **...**

solution/0200-0299/0257.Binary Tree Paths/README_EN.md

+54-2
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,65 @@
4343
### **Python3**
4444

4545
```python
46-
46+
# Definition for a binary tree node.
47+
# class TreeNode:
48+
# def __init__(self, x):
49+
# self.val = x
50+
# self.left = None
51+
# self.right = None
52+
53+
class Solution:
54+
def binaryTreePaths(self, root: TreeNode) -> List[str]:
55+
def dfs(root):
56+
if root is None:
57+
return
58+
path.append(str(root.val))
59+
if root.left is None and root.right is None:
60+
res.append("->".join(path))
61+
dfs(root.left)
62+
dfs(root.right)
63+
path.pop()
64+
res = []
65+
path = []
66+
dfs(root)
67+
return res
4768
```
4869

4970
### **Java**
5071

5172
```java
52-
73+
/**
74+
* Definition for a binary tree node.
75+
* public class TreeNode {
76+
* int val;
77+
* TreeNode left;
78+
* TreeNode right;
79+
* TreeNode(int x) { val = x; }
80+
* }
81+
*/
82+
class Solution {
83+
private List<String> res;
84+
private List<String> path;
85+
86+
public List<String> binaryTreePaths(TreeNode root) {
87+
if (root == null) return Collections.emptyList();
88+
res = new ArrayList<>();
89+
path = new ArrayList<>();
90+
dfs(root);
91+
return res;
92+
}
93+
94+
private void dfs(TreeNode root) {
95+
if (root == null) return;
96+
path.add(String.valueOf(root.val));
97+
if (root.left == null && root.right == null) {
98+
res.add(String.join("->", path));
99+
}
100+
dfs(root.left);
101+
dfs(root.right);
102+
path.remove(path.size() - 1);
103+
}
104+
}
53105
```
54106

55107
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
110
class Solution {
11+
private List<String> res;
12+
private List<String> path;
13+
214
public List<String> binaryTreePaths(TreeNode root) {
3-
List<String> res = new ArrayList<>();
4-
solution(root, "", res);
15+
if (root == null) return Collections.emptyList();
16+
res = new ArrayList<>();
17+
path = new ArrayList<>();
18+
dfs(root);
519
return res;
620
}
721

8-
private void solution(TreeNode root, String path, List<String> res) {
9-
if (root != null) {
10-
path += root.val;
11-
if (root.left == null && root.right == null) {
12-
res.add(path);
13-
} else {
14-
path += "->";
15-
solution(root.left, path, res);
16-
solution(root.right, path, res);
17-
}
22+
private void dfs(TreeNode root) {
23+
if (root == null) return;
24+
path.add(String.valueOf(root.val));
25+
if (root.left == null && root.right == null) {
26+
res.add(String.join("->", path));
1827
}
28+
dfs(root.left);
29+
dfs(root.right);
30+
path.remove(path.size() - 1);
1931
}
20-
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution:
9+
def binaryTreePaths(self, root: TreeNode) -> List[str]:
10+
def dfs(root):
11+
if root is None:
12+
return
13+
path.append(str(root.val))
14+
if root.left is None and root.right is None:
15+
res.append("->".join(path))
16+
dfs(root.left)
17+
dfs(root.right)
18+
path.pop()
19+
res = []
20+
path = []
21+
dfs(root)
22+
return res

0 commit comments

Comments
 (0)