From da742feaa3e324c9489bedaf867004cae583cc90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98windscape=E2=80=99?= <2462269317@qq.com> Date: Thu, 23 Jan 2025 11:47:27 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BF=AE=E6=94=B90112.=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E6=80=BB=E5=92=8C.md=E7=9A=84Java=E7=89=88=E6=9C=AC=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E7=9A=84=E5=AD=97=E6=AF=8D=E5=B0=8F=E5=86=99=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0112.路径总和.md | 59 ++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/problems/0112.路径总和.md b/problems/0112.路径总和.md index b97013e6..141967f5 100644 --- a/problems/0112.路径总和.md +++ b/problems/0112.路径总和.md @@ -309,25 +309,25 @@ public: 0112.路径总和 ```java -class solution { - public boolean haspathsum(treenode root, int targetsum) { +class Solution { + public boolean hasPathSum(TreeNode root, int targetSum) { if (root == null) { return false; } - targetsum -= root.val; + targetSum -= root.val; // 叶子结点 if (root.left == null && root.right == null) { - return targetsum == 0; + return targetSum == 0; } if (root.left != null) { - boolean left = haspathsum(root.left, targetsum); - if (left) { // 已经找到 + boolean left = hasPathSum(root.left, targetSum); + if (left) { // 已经找到,提前返回 return true; } } if (root.right != null) { - boolean right = haspathsum(root.right, targetsum); - if (right) { // 已经找到 + boolean right = hasPathSum(root.right, targetSum); + if (right) { // 已经找到,提前返回 return true; } } @@ -336,16 +336,16 @@ class solution { } // lc112 简洁方法 -class solution { - public boolean haspathsum(treenode root, int targetsum) { +class Solution { + public boolean hasPathSum(TreeNode root, int targetSum) { 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 -class solution { - public boolean haspathsum(treenode root, int targetsum) { +class Solution { + public boolean hasPathSum(TreeNode root, int targetSum) { if(root == null) return false; - stack stack1 = new stack<>(); - stack stack2 = new stack<>(); + Stack stack1 = new Stack<>(); + Stack stack2 = new Stack<>(); stack1.push(root); stack2.push(root.val); - while(!stack1.isempty()) { + while(!stack1.isEmpty()) { int size = stack1.size(); for(int i = 0; i < size; i++) { - treenode node = stack1.pop(); + TreeNode node = stack1.pop(); int sum = stack2.pop(); // 如果该节点是叶子节点了,同时该节点的路径数值等于sum,那么就返回true - if(node.left == null && node.right == null && sum == targetsum) { + if(node.left == null && node.right == null && sum == targetSum) { return true; } // 右节点,压进去一个节点的时候,将该节点的路径数值也记录下来 @@ -387,8 +387,9 @@ class solution { } } ``` -```Java 統一迭代法 - public boolean hasPathSum(TreeNode root, int targetSum) { +```Java +class Solution { + public boolean hasPathSum(TreeNode root, int targetSum) { Stack treeNodeStack = new Stack<>(); Stack sumStack = new Stack<>(); @@ -422,38 +423,39 @@ class solution { } return false; } +} ``` 0113.路径总和-ii ```java -class solution { - public List> pathsum(TreeNode root, int targetsum) { +class Solution { + public List> pathSum(TreeNode root, int targetSum) { List> res = new ArrayList<>(); if (root == null) return res; // 非空判断 List path = new LinkedList<>(); - preorderdfs(root, targetsum, res, path); + preOrderDfs(root, targetSum, res, path); return res; } - public void preorderdfs(TreeNode root, int targetsum, List> res, List path) { + public void preOrderDfs(TreeNode root, int targetSum, List> res, List path) { path.add(root.val); // 遇到了叶子节点 if (root.left == null && root.right == null) { // 找到了和为 targetsum 的路径 - if (targetsum - root.val == 0) { + if (targetSum - root.val == 0) { res.add(new ArrayList<>(path)); } return; // 如果和不为 targetsum,返回 } 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); // 回溯 } 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); // 回溯 } } @@ -1626,3 +1628,4 @@ public class Solution { + From e6698cbac457714427f4a9cba6ea2a3ea9eed94c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98windscape=E2=80=99?= <2462269317@qq.com> Date: Sat, 25 Jan 2025 11:52:09 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E6=94=B90530.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E7=BB=9D?= =?UTF-8?q?=E5=AF=B9=E5=B7=AE.md=E7=9A=84Java=E7=89=88=E6=9C=AC=20?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E4=BA=86=E4=BB=A3=E7=A0=81=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E5=8C=96=E5=B9=B6=E6=B7=BB=E5=8A=A0=E4=BA=86=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E8=BF=AD=E4=BB=A3=E6=B3=95=E7=9A=84=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0530.二叉搜索树的最小绝对差.md | 46 +++++++++++++++---------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/problems/0530.二叉搜索树的最小绝对差.md b/problems/0530.二叉搜索树的最小绝对差.md index 2533a618..b6d08dbe 100644 --- a/problems/0530.二叉搜索树的最小绝对差.md +++ b/problems/0530.二叉搜索树的最小绝对差.md @@ -153,23 +153,27 @@ public: 递归 ```java class Solution { - TreeNode pre;// 记录上一个遍历的结点 + TreeNode pre; // 记录上一个遍历的结点 int result = Integer.MAX_VALUE; + public int getMinimumDifference(TreeNode root) { - if(root==null)return 0; - traversal(root); - return result; + if (root == null) + return 0; + 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); - //中 - if(pre!=null){ - result = Math.min(result,root.val-pre.val); + // 中 + if (pre != null) { + result = Math.min(result, root.val - pre.val); } pre = root; - //右 + // 右 traversal(root.right); } } @@ -182,22 +186,27 @@ class Solution { TreeNode pre = null; int result = Integer.MAX_VALUE; - if(root != null) + if (root != null) stack.add(root); - while(!stack.isEmpty()){ + + // 中序遍历(左中右),由于栈先入后出,反序(右中左) + while (!stack.isEmpty()) { TreeNode curr = stack.peek(); - if(curr != null){ + if (curr != null) { stack.pop(); - if(curr.right != null) + // 右 + if (curr.right != null) stack.add(curr.right); + // 中(先用null标记) stack.add(curr); stack.add(null); - if(curr.left != null) + // 左 + if (curr.left != null) stack.add(curr.left); - }else{ + } else { // 中(遇到null再处理) stack.pop(); TreeNode temp = stack.pop(); - if(pre != null) + if (pre != null) result = Math.min(result, temp.val - pre.val); pre = temp; } @@ -674,3 +683,4 @@ public class Solution + From 333099a1268544306e303fcb779ffd801c0d0c92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98windscape=E2=80=99?= <2462269317@qq.com> Date: Mon, 3 Feb 2025 20:13:41 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E6=80=BB=E7=BB=93=E7=AF=87.md=E4=B8=80=E5=A4=84?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E7=BA=A7=E5=88=AB=E7=9A=84=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/二叉树总结篇.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/problems/二叉树总结篇.md b/problems/二叉树总结篇.md index 8db40d65..4794233a 100644 --- a/problems/二叉树总结篇.md +++ b/problems/二叉树总结篇.md @@ -92,10 +92,9 @@ * 递归:中序,双指针操作 * 迭代:模拟中序,逻辑相同 * [求二叉搜索树的众数](https://programmercarl.com/0501.二叉搜索树中的众数.html) - + * 递归:中序,清空结果集的技巧,遍历一遍便可求众数集合 - * [二叉搜索树转成累加树](https://programmercarl.com/0538.把二叉搜索树转换为累加树.html) - +* [二叉搜索树转成累加树](https://programmercarl.com/0538.把二叉搜索树转换为累加树.html) * 递归:中序,双指针操作累加 * 迭代:模拟中序,逻辑相同 @@ -163,3 +162,4 @@ +