Skip to content

Commit 74e30de

Browse files
committed
feat: update solutions to lc problem: No.0257
No.0257.Binary Tree Paths
1 parent 801a22e commit 74e30de

File tree

7 files changed

+310
-107
lines changed

7 files changed

+310
-107
lines changed

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

+106-35
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,26 @@
3939
```python
4040
# Definition for a binary tree node.
4141
# class TreeNode:
42-
# def __init__(self, x):
43-
# self.val = x
44-
# self.left = None
45-
# self.right = None
46-
42+
# def __init__(self, val=0, left=None, right=None):
43+
# self.val = val
44+
# self.left = left
45+
# self.right = right
4746
class Solution:
4847
def binaryTreePaths(self, root: TreeNode) -> List[str]:
4948
def dfs(root):
5049
if root is None:
5150
return
52-
path.append(str(root.val))
51+
t.append(str(root.val))
5352
if root.left is None and root.right is None:
54-
res.append("->".join(path))
53+
ans.append('->'.join(t))
5554
dfs(root.left)
5655
dfs(root.right)
57-
path.pop()
58-
res = []
59-
path = []
56+
t.pop()
57+
58+
t = []
59+
ans = []
6060
dfs(root)
61-
return res
61+
return ans
6262
```
6363

6464
### **Java**
@@ -72,30 +72,37 @@ class Solution:
7272
* int val;
7373
* TreeNode left;
7474
* TreeNode right;
75-
* TreeNode(int x) { val = x; }
75+
* TreeNode() {}
76+
* TreeNode(int val) { this.val = val; }
77+
* TreeNode(int val, TreeNode left, TreeNode right) {
78+
* this.val = val;
79+
* this.left = left;
80+
* this.right = right;
81+
* }
7682
* }
7783
*/
7884
class Solution {
79-
private List<String> res;
80-
private List<String> path;
85+
private List<String> ans;
86+
private List<String> t;
8187

8288
public List<String> binaryTreePaths(TreeNode root) {
83-
if (root == null) return Collections.emptyList();
84-
res = new ArrayList<>();
85-
path = new ArrayList<>();
89+
ans = new ArrayList<>();
90+
t = new ArrayList<>();
8691
dfs(root);
87-
return res;
92+
return ans;
8893
}
8994

9095
private void dfs(TreeNode root) {
91-
if (root == null) return;
92-
path.add(String.valueOf(root.val));
96+
if (root == null) {
97+
return;
98+
}
99+
t.add(root.val + "");
93100
if (root.left == null && root.right == null) {
94-
res.add(String.join("->", path));
101+
ans.add(String.join("->", t));
95102
}
96103
dfs(root.left);
97104
dfs(root.right);
98-
path.remove(path.size() - 1);
105+
t.remove(t.size() - 1);
99106
}
100107
}
101108
```
@@ -119,25 +126,89 @@ class Solution {
119126

120127
function binaryTreePaths(root: TreeNode | null): string[] {
121128
let ans = [];
122-
let pre = "";
123-
dfs(root, pre, ans);
129+
let t = [];
130+
function dfs(root) {
131+
if (!root) return;
132+
t.push(String(root.val));
133+
if (!root.left && !root.right) ans.push(t.join('->'));
134+
dfs(root.left);
135+
dfs(root.right);
136+
t.pop();
137+
}
138+
dfs(root);
124139
return ans;
125140
}
141+
```
126142

127-
function dfs(root: TreeNode | null, pre: string, ans: string[]): void {
128-
if (root == null) return;
129-
let val = String(root.val);
130-
pre = pre.length > 0 ? `${pre}->${val}` : pre + val;
131-
// 叶子节点
132-
if (root.left == null && root.right == null) {
133-
ans.push(pre);
134-
return;
135-
}
136-
dfs(root.left, pre, ans);
137-
dfs(root.right, pre, ans);
143+
### **Go**
144+
145+
```go
146+
/**
147+
* Definition for a binary tree node.
148+
* type TreeNode struct {
149+
* Val int
150+
* Left *TreeNode
151+
* Right *TreeNode
152+
* }
153+
*/
154+
func binaryTreePaths(root *TreeNode) []string {
155+
var ans []string
156+
var t []string
157+
var dfs func(root *TreeNode)
158+
dfs = func(root *TreeNode) {
159+
if root == nil {
160+
return
161+
}
162+
t = append(t, strconv.Itoa(root.Val))
163+
if root.Left == nil && root.Right == nil {
164+
ans = append(ans, strings.Join(t, "->"))
165+
}
166+
dfs(root.Left)
167+
dfs(root.Right)
168+
t = t[:len(t)-1]
169+
}
170+
dfs(root)
171+
return ans
138172
}
139173
```
140174

175+
### **C++**
176+
177+
```cpp
178+
/**
179+
* Definition for a binary tree node.
180+
* struct TreeNode {
181+
* int val;
182+
* TreeNode *left;
183+
* TreeNode *right;
184+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
185+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
186+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
187+
* };
188+
*/
189+
class Solution {
190+
public:
191+
vector<string> ans;
192+
193+
vector<string> binaryTreePaths(TreeNode* root) {
194+
dfs(root, "");
195+
return ans;
196+
}
197+
198+
void dfs(TreeNode* root, string t) {
199+
t += to_string(root->val);
200+
if (!root->left && !root->right)
201+
{
202+
ans.push_back(t);
203+
return;
204+
}
205+
t += "->";
206+
if (root->left) dfs(root->left, t);
207+
if (root->right) dfs(root->right, t);
208+
}
209+
};
210+
```
211+
141212
### **...**
142213
143214
```

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

+106-35
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,26 @@
4040
```python
4141
# Definition for a binary tree node.
4242
# class TreeNode:
43-
# def __init__(self, x):
44-
# self.val = x
45-
# self.left = None
46-
# self.right = None
47-
43+
# def __init__(self, val=0, left=None, right=None):
44+
# self.val = val
45+
# self.left = left
46+
# self.right = right
4847
class Solution:
4948
def binaryTreePaths(self, root: TreeNode) -> List[str]:
5049
def dfs(root):
5150
if root is None:
5251
return
53-
path.append(str(root.val))
52+
t.append(str(root.val))
5453
if root.left is None and root.right is None:
55-
res.append("->".join(path))
54+
ans.append('->'.join(t))
5655
dfs(root.left)
5756
dfs(root.right)
58-
path.pop()
59-
res = []
60-
path = []
57+
t.pop()
58+
59+
t = []
60+
ans = []
6161
dfs(root)
62-
return res
62+
return ans
6363
```
6464

6565
### **Java**
@@ -71,30 +71,37 @@ class Solution:
7171
* int val;
7272
* TreeNode left;
7373
* TreeNode right;
74-
* TreeNode(int x) { val = x; }
74+
* TreeNode() {}
75+
* TreeNode(int val) { this.val = val; }
76+
* TreeNode(int val, TreeNode left, TreeNode right) {
77+
* this.val = val;
78+
* this.left = left;
79+
* this.right = right;
80+
* }
7581
* }
7682
*/
7783
class Solution {
78-
private List<String> res;
79-
private List<String> path;
84+
private List<String> ans;
85+
private List<String> t;
8086

8187
public List<String> binaryTreePaths(TreeNode root) {
82-
if (root == null) return Collections.emptyList();
83-
res = new ArrayList<>();
84-
path = new ArrayList<>();
88+
ans = new ArrayList<>();
89+
t = new ArrayList<>();
8590
dfs(root);
86-
return res;
91+
return ans;
8792
}
8893

8994
private void dfs(TreeNode root) {
90-
if (root == null) return;
91-
path.add(String.valueOf(root.val));
95+
if (root == null) {
96+
return;
97+
}
98+
t.add(root.val + "");
9299
if (root.left == null && root.right == null) {
93-
res.add(String.join("->", path));
100+
ans.add(String.join("->", t));
94101
}
95102
dfs(root.left);
96103
dfs(root.right);
97-
path.remove(path.size() - 1);
104+
t.remove(t.size() - 1);
98105
}
99106
}
100107
```
@@ -118,25 +125,89 @@ class Solution {
118125

119126
function binaryTreePaths(root: TreeNode | null): string[] {
120127
let ans = [];
121-
let pre = "";
122-
dfs(root, pre, ans);
128+
let t = [];
129+
function dfs(root) {
130+
if (!root) return;
131+
t.push(String(root.val));
132+
if (!root.left && !root.right) ans.push(t.join('->'));
133+
dfs(root.left);
134+
dfs(root.right);
135+
t.pop();
136+
}
137+
dfs(root);
123138
return ans;
124139
}
140+
```
125141

126-
function dfs(root: TreeNode | null, pre: string, ans: string[]): void {
127-
if (root == null) return;
128-
let val = String(root.val);
129-
pre = pre.length > 0 ? `${pre}->${val}` : pre + val;
130-
// 叶子节点
131-
if (root.left == null && root.right == null) {
132-
ans.push(pre);
133-
return;
134-
}
135-
dfs(root.left, pre, ans);
136-
dfs(root.right, pre, ans);
142+
### **Go**
143+
144+
```go
145+
/**
146+
* Definition for a binary tree node.
147+
* type TreeNode struct {
148+
* Val int
149+
* Left *TreeNode
150+
* Right *TreeNode
151+
* }
152+
*/
153+
func binaryTreePaths(root *TreeNode) []string {
154+
var ans []string
155+
var t []string
156+
var dfs func(root *TreeNode)
157+
dfs = func(root *TreeNode) {
158+
if root == nil {
159+
return
160+
}
161+
t = append(t, strconv.Itoa(root.Val))
162+
if root.Left == nil && root.Right == nil {
163+
ans = append(ans, strings.Join(t, "->"))
164+
}
165+
dfs(root.Left)
166+
dfs(root.Right)
167+
t = t[:len(t)-1]
168+
}
169+
dfs(root)
170+
return ans
137171
}
138172
```
139173

174+
### **C++**
175+
176+
```cpp
177+
/**
178+
* Definition for a binary tree node.
179+
* struct TreeNode {
180+
* int val;
181+
* TreeNode *left;
182+
* TreeNode *right;
183+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
184+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
185+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
186+
* };
187+
*/
188+
class Solution {
189+
public:
190+
vector<string> ans;
191+
192+
vector<string> binaryTreePaths(TreeNode* root) {
193+
dfs(root, "");
194+
return ans;
195+
}
196+
197+
void dfs(TreeNode* root, string t) {
198+
t += to_string(root->val);
199+
if (!root->left && !root->right)
200+
{
201+
ans.push_back(t);
202+
return;
203+
}
204+
t += "->";
205+
if (root->left) dfs(root->left, t);
206+
if (root->right) dfs(root->right, t);
207+
}
208+
};
209+
```
210+
140211
### **...**
141212
142213
```

0 commit comments

Comments
 (0)