Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbed3394

Browse files
Merge pull requestyoungyangyang04#449 from KailokFung/master
feat(二叉树理论基础.md): 新增java版本
2 parentsc41cb46 +59403e0 commitbed3394

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed

‎problems/0226.翻转二叉树.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ public:
205205
Java:
206206

207207
```Java
208+
//DFS递归
208209
classSolution {
209210
/**
210211
* 前后序遍历都可以
@@ -226,6 +227,31 @@ class Solution {
226227
root.right= tmp;
227228
}
228229
}
230+
231+
//BFS
232+
classSolution {
233+
publicTreeNodeinvertTree(TreeNoderoot) {
234+
if (root==null) {returnnull;}
235+
ArrayDeque<TreeNode> deque=newArrayDeque<>();
236+
deque.offer(root);
237+
while (!deque.isEmpty()) {
238+
int size= deque.size();
239+
while (size-->0) {
240+
TreeNode node= deque.poll();
241+
swap(node);
242+
if (node.left!=null) {deque.offer(node.left);}
243+
if (node.right!=null) {deque.offer(node.right);}
244+
}
245+
}
246+
return root;
247+
}
248+
249+
publicvoidswap(TreeNoderoot) {
250+
TreeNode temp= root.left;
251+
root.left= root.right;
252+
root.right= temp;
253+
}
254+
}
229255
```
230256

231257
Python:

‎problems/0257.二叉树的所有路径.md

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ public:
283283
Java:
284284

285285
```Java
286+
//解法一
286287
classSolution {
287288
/**
288289
* 递归法
@@ -321,6 +322,52 @@ class Solution {
321322
}
322323
}
323324

325+
//解法二(常规前序遍历,不用回溯),更容易理解
326+
classSolution {
327+
publicList<String>binaryTreePaths(TreeNoderoot) {
328+
List<String> res=newArrayList<>();
329+
helper(root,newStringBuilder(), res);
330+
return res;
331+
}
332+
333+
publicvoidhelper(TreeNoderoot,StringBuildersb,List<String>res) {
334+
if (root==null) {return;}
335+
// 遇到叶子结点就放入当前路径到res集合中
336+
if (root.left==null&& root.right==null) {
337+
sb.append(root.val);
338+
res.add(sb.toString());
339+
// 记得结束当前方法
340+
return;
341+
}
342+
helper(root.left,newStringBuilder(sb).append(root.val+"->"),res);
343+
helper(root.right,newStringBuilder(sb).append(root.val+"->"),res);
344+
}
345+
}
346+
347+
//针对解法二优化,思路本质是一样的
348+
classSolution {
349+
publicList<String>binaryTreePaths(TreeNoderoot) {
350+
List<String> res=newArrayList<>();
351+
helper(root,"", res);
352+
return res;
353+
}
354+
355+
publicvoidhelper(TreeNoderoot,Stringpath,List<String>res) {
356+
if (root==null) {return;}
357+
// 由原始解法二可以知道,root的值肯定会下面某一个条件加入到path中,那么干脆直接在这一步加入即可
358+
StringBuilder sb=newStringBuilder(path);
359+
sb.append(root.val);
360+
if (root.left==null&& root.right==null) {
361+
res.add(sb.toString());
362+
}else{
363+
// 如果是非叶子结点则还需要跟上一个 “->”
364+
sb.append("->");
365+
helper(root.left,sb.toString(),res);
366+
helper(root.right,sb.toString(),res);
367+
}
368+
}
369+
}
370+
324371
```
325372

326373
Python:
@@ -350,7 +397,7 @@ class Solution:
350397

351398
```
352399
Go:
353-
400+
354401
```go
355402
funcbinaryTreePaths(root *TreeNode) []string {
356403
res:=make([]string,0)

‎problems/二叉树理论基础.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,21 @@ struct TreeNode {
188188

189189
Java:
190190

191+
```java
192+
publicclassTreeNode {
193+
int val;
194+
TreeNode left;
195+
TreeNode right;
196+
TreeNode() {}
197+
TreeNode(intval) {this.val= val; }
198+
TreeNode(intval,TreeNodeleft,TreeNoderight) {
199+
this.val= val;
200+
this.left= left;
201+
this.right= right;
202+
}
203+
}
204+
```
205+
191206

192207
Python:
193208

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp