Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(二叉树理论基础.md): 新增java版本 #449

Merged
merged 8 commits into from
Jul 3, 2021
26 changes: 26 additions & 0 deletions problems/0226.翻转二叉树.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ public:
Java:

```Java
//DFS递归
class Solution {
/**
* 前后序遍历都可以
Expand All @@ -226,6 +227,31 @@ class Solution {
root.right = tmp;
}
}

//BFS
class Solution {
public TreeNode invertTree(TreeNode root) {
if (root == null) {return null;}
ArrayDeque<TreeNode> deque = new ArrayDeque<>();
deque.offer(root);
while (!deque.isEmpty()) {
int size = deque.size();
while (size-- > 0) {
TreeNode node = deque.poll();
swap(node);
if (node.left != null) {deque.offer(node.left);}
if (node.right != null) {deque.offer(node.right);}
}
}
return root;
}

public void swap(TreeNode root) {
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
}
}
```

Python:
Expand Down
49 changes: 48 additions & 1 deletion problems/0257.二叉树的所有路径.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ public:
Java:

```Java
//解法一
class Solution {
/**
* 递归法
Expand Down Expand Up @@ -321,6 +322,52 @@ class Solution {
}
}

//解法二(常规前序遍历,不用回溯),更容易理解
class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> res = new ArrayList<>();
helper(root, new StringBuilder(), res);
return res;
}

public void helper(TreeNode root, StringBuilder sb, List<String> res) {
if (root == null) {return;}
// 遇到叶子结点就放入当前路径到res集合中
if (root.left == null && root.right ==null) {
sb.append(root.val);
res.add(sb.toString());
// 记得结束当前方法
return;
}
helper(root.left,new StringBuilder(sb).append(root.val + "->"),res);
helper(root.right,new StringBuilder(sb).append(root.val + "->"),res);
}
}

//针对解法二优化,思路本质是一样的
class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> res = new ArrayList<>();
helper(root, "", res);
return res;
}

public void helper(TreeNode root, String path, List<String> res) {
if (root == null) {return;}
// 由原始解法二可以知道,root的值肯定会下面某一个条件加入到path中,那么干脆直接在这一步加入即可
StringBuilder sb = new StringBuilder(path);
sb.append(root.val);
if (root.left == null && root.right ==null) {
res.add(sb.toString());
}else{
// 如果是非叶子结点则还需要跟上一个 “->”
sb.append("->");
helper(root.left,sb.toString(),res);
helper(root.right,sb.toString(),res);
}
}
}

```

Python:
Expand Down Expand Up @@ -350,7 +397,7 @@ class Solution:

```
Go:

```go
func binaryTreePaths(root *TreeNode) []string {
res := make([]string, 0)
Expand Down
15 changes: 15 additions & 0 deletions problems/二叉树理论基础.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,21 @@ struct TreeNode {

Java:

```java
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {}
TreeNode(int val) { this.val = val; }
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
```


Python:

Expand Down