Merge pull request #2874 from curforever/master

修改0112.路径总和.md的Java版本代码的字母小写问题、修改0530.二叉搜索树的最小绝对差.md的Java版本 进行了代码格式化并添加了统一迭代法的注释、修改二叉树总结篇.md一处列表级别的格式问题
This commit is contained in:
程序员Carl
2025-03-05 11:55:42 +08:00
committed by GitHub
3 changed files with 62 additions and 49 deletions

View File

@ -309,25 +309,25 @@ public:
0112.路径总和 0112.路径总和
```java ```java
class solution { class Solution {
public boolean haspathsum(treenode root, int targetsum) { public boolean hasPathSum(TreeNode root, int targetSum) {
if (root == null) { if (root == null) {
return false; return false;
} }
targetsum -= root.val; targetSum -= root.val;
// 叶子结点 // 叶子结点
if (root.left == null && root.right == null) { if (root.left == null && root.right == null) {
return targetsum == 0; return targetSum == 0;
} }
if (root.left != null) { if (root.left != null) {
boolean left = haspathsum(root.left, targetsum); boolean left = hasPathSum(root.left, targetSum);
if (left) { // 已经找到 if (left) { // 已经找到,提前返回
return true; return true;
} }
} }
if (root.right != null) { if (root.right != null) {
boolean right = haspathsum(root.right, targetsum); boolean right = hasPathSum(root.right, targetSum);
if (right) { // 已经找到 if (right) { // 已经找到,提前返回
return true; return true;
} }
} }
@ -336,16 +336,16 @@ class solution {
} }
// lc112 简洁方法 // lc112 简洁方法
class solution { class Solution {
public boolean haspathsum(treenode root, int targetsum) { public boolean hasPathSum(TreeNode root, int targetSum) {
if (root == null) return false; // 为空退出 if (root == null) return false; // 为空退出
// 叶子节点判断是否符合 // 叶子节点判断是否符合
if (root.left == null && root.right == null) return root.val == targetsum; if (root.left == null && root.right == null) return root.val == targetSum;
// 求两侧分支的路径和 // 求两侧分支的路径和
return haspathsum(root.left, targetsum - root.val) || haspathsum(root.right, targetsum - root.val); return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val);
} }
} }
``` ```
@ -353,22 +353,22 @@ class solution {
迭代 迭代
```java ```java
class solution { class Solution {
public boolean haspathsum(treenode root, int targetsum) { public boolean hasPathSum(TreeNode root, int targetSum) {
if(root == null) return false; if(root == null) return false;
stack<treenode> stack1 = new stack<>(); Stack<TreeNode> stack1 = new Stack<>();
stack<integer> stack2 = new stack<>(); Stack<Integer> stack2 = new Stack<>();
stack1.push(root); stack1.push(root);
stack2.push(root.val); stack2.push(root.val);
while(!stack1.isempty()) { while(!stack1.isEmpty()) {
int size = stack1.size(); int size = stack1.size();
for(int i = 0; i < size; i++) { for(int i = 0; i < size; i++) {
treenode node = stack1.pop(); TreeNode node = stack1.pop();
int sum = stack2.pop(); int sum = stack2.pop();
// 如果该节点是叶子节点了同时该节点的路径数值等于sum那么就返回true // 如果该节点是叶子节点了同时该节点的路径数值等于sum那么就返回true
if(node.left == null && node.right == null && sum == targetsum) { if(node.left == null && node.right == null && sum == targetSum) {
return true; return true;
} }
// 右节点,压进去一个节点的时候,将该节点的路径数值也记录下来 // 右节点,压进去一个节点的时候,将该节点的路径数值也记录下来
@ -387,8 +387,9 @@ class solution {
} }
} }
``` ```
```Java 統一迭代法 ```Java
public boolean hasPathSum(TreeNode root, int targetSum) { class Solution {
public boolean hasPathSum(TreeNode root, int targetSum) {
Stack<TreeNode> treeNodeStack = new Stack<>(); Stack<TreeNode> treeNodeStack = new Stack<>();
Stack<Integer> sumStack = new Stack<>(); Stack<Integer> sumStack = new Stack<>();
@ -422,38 +423,39 @@ class solution {
} }
return false; return false;
} }
}
``` ```
0113.路径总和-ii 0113.路径总和-ii
```java ```java
class solution { class Solution {
public List<List<Integer>> pathsum(TreeNode root, int targetsum) { public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
List<List<Integer>> res = new ArrayList<>(); List<List<Integer>> res = new ArrayList<>();
if (root == null) return res; // 非空判断 if (root == null) return res; // 非空判断
List<Integer> path = new LinkedList<>(); List<Integer> path = new LinkedList<>();
preorderdfs(root, targetsum, res, path); preOrderDfs(root, targetSum, res, path);
return res; return res;
} }
public void preorderdfs(TreeNode root, int targetsum, List<List<Integer>> res, List<Integer> path) { public void preOrderDfs(TreeNode root, int targetSum, List<List<Integer>> res, List<Integer> path) {
path.add(root.val); path.add(root.val);
// 遇到了叶子节点 // 遇到了叶子节点
if (root.left == null && root.right == null) { if (root.left == null && root.right == null) {
// 找到了和为 targetsum 的路径 // 找到了和为 targetsum 的路径
if (targetsum - root.val == 0) { if (targetSum - root.val == 0) {
res.add(new ArrayList<>(path)); res.add(new ArrayList<>(path));
} }
return; // 如果和不为 targetsum返回 return; // 如果和不为 targetsum返回
} }
if (root.left != null) { if (root.left != null) {
preorderdfs(root.left, targetsum - root.val, res, path); preOrderDfs(root.left, targetSum - root.val, res, path);
path.remove(path.size() - 1); // 回溯 path.remove(path.size() - 1); // 回溯
} }
if (root.right != null) { if (root.right != null) {
preorderdfs(root.right, targetsum - root.val, res, path); preOrderDfs(root.right, targetSum - root.val, res, path);
path.remove(path.size() - 1); // 回溯 path.remove(path.size() - 1); // 回溯
} }
} }
@ -1626,3 +1628,4 @@ public class Solution {
<a href="https://programmercarl.com/other/kstar.html" target="_blank"> <a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/> <img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a> </a>

View File

@ -153,23 +153,27 @@ public:
递归 递归
```java ```java
class Solution { class Solution {
TreeNode pre;// 记录上一个遍历的结点 TreeNode pre; // 记录上一个遍历的结点
int result = Integer.MAX_VALUE; int result = Integer.MAX_VALUE;
public int getMinimumDifference(TreeNode root) { public int getMinimumDifference(TreeNode root) {
if(root==null)return 0; if (root == null)
traversal(root); return 0;
return result; traversal(root);
return result;
} }
public void traversal(TreeNode root){
if(root==null)return; public void traversal(TreeNode root) {
//左 if (root == null)
return;
// 左
traversal(root.left); traversal(root.left);
//中 //
if(pre!=null){ if (pre != null) {
result = Math.min(result,root.val-pre.val); result = Math.min(result, root.val - pre.val);
} }
pre = root; pre = root;
//右 //
traversal(root.right); traversal(root.right);
} }
} }
@ -182,22 +186,27 @@ class Solution {
TreeNode pre = null; TreeNode pre = null;
int result = Integer.MAX_VALUE; int result = Integer.MAX_VALUE;
if(root != null) if (root != null)
stack.add(root); stack.add(root);
while(!stack.isEmpty()){
// 中序遍历(左中右),由于栈先入后出,反序(右中左)
while (!stack.isEmpty()) {
TreeNode curr = stack.peek(); TreeNode curr = stack.peek();
if(curr != null){ if (curr != null) {
stack.pop(); stack.pop();
if(curr.right != null) // 右
if (curr.right != null)
stack.add(curr.right); stack.add(curr.right);
// 中先用null标记
stack.add(curr); stack.add(curr);
stack.add(null); stack.add(null);
if(curr.left != null) // 左
if (curr.left != null)
stack.add(curr.left); stack.add(curr.left);
}else{ } else { // 中遇到null再处理
stack.pop(); stack.pop();
TreeNode temp = stack.pop(); TreeNode temp = stack.pop();
if(pre != null) if (pre != null)
result = Math.min(result, temp.val - pre.val); result = Math.min(result, temp.val - pre.val);
pre = temp; pre = temp;
} }
@ -674,3 +683,4 @@ public class Solution
<a href="https://programmercarl.com/other/kstar.html" target="_blank"> <a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/> <img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a> </a>

View File

@ -92,10 +92,9 @@
* 递归:中序,双指针操作 * 递归:中序,双指针操作
* 迭代:模拟中序,逻辑相同 * 迭代:模拟中序,逻辑相同
* [求二叉搜索树的众数](https://programmercarl.com/0501.二叉搜索树中的众数.html) * [求二叉搜索树的众数](https://programmercarl.com/0501.二叉搜索树中的众数.html)
* 递归:中序,清空结果集的技巧,遍历一遍便可求众数集合 * 递归:中序,清空结果集的技巧,遍历一遍便可求众数集合
* [二叉搜索树转成累加树](https://programmercarl.com/0538.把二叉搜索树转换为累加树.html) * [二叉搜索树转成累加树](https://programmercarl.com/0538.把二叉搜索树转换为累加树.html)
* 递归:中序,双指针操作累加 * 递归:中序,双指针操作累加
* 迭代:模拟中序,逻辑相同 * 迭代:模拟中序,逻辑相同
@ -163,3 +162,4 @@
<a href="https://programmercarl.com/other/kstar.html" target="_blank"> <a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/> <img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a> </a>