From 247dc563440cbe9d4427cd9c38642d128ea847a6 Mon Sep 17 00:00:00 2001 From: UserXSX <732209117@qq.com> Date: Thu, 1 May 2025 10:47:38 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=200054.=E6=9B=BF?= =?UTF-8?q?=E6=8D=A2=E6=95=B0=E5=AD=97.md=20Java=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E8=A7=A3=E6=B3=95=E4=BA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" "b/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" index 665e8ecbae..a6d3b15511 100644 --- "a/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" +++ "b/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" @@ -189,7 +189,7 @@ public class Main { String s = sc.next(); int len = s.length(); for (int i = 0; i < s.length(); i++) { - if (s.charAt(i) >= 0 && s.charAt(i) <= '9') { + if (s.charAt(i) >= '0' && s.charAt(i) <= '9') { len += 5; } } From a7395c9f842cddcee1a4d875b75a0142235a690a Mon Sep 17 00:00:00 2001 From: UserXSX <732209117@qq.com> Date: Mon, 7 Jul 2025 12:14:20 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E7=BB=9F=E4=B8=80=E8=BF=AD=E4=BB=A3=E6=B3=95?= =?UTF-8?q?.md=20Java=E7=89=88=E6=9C=ACboolean=E6=A0=87=E8=AE=B0=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\350\277\255\344\273\243\346\263\225.md" | 109 +++++++++++++++++- 1 file changed, 106 insertions(+), 3 deletions(-) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" index 803b25ae80..305a11aaac 100755 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" @@ -225,7 +225,7 @@ public: ## 其他语言版本 ### Java: -迭代法前序遍历代码如下: +迭代法前序遍历(空指针标记法)代码如下: ```java class Solution { @@ -254,7 +254,42 @@ class Solution { } ``` -迭代法中序遍历代码如下: +迭代法前序遍历(boolean标记法)代码如下: + +```java +class Solution { + public List preorderTraversal(TreeNode root) { + List res = new LinkedList<>(); + if (root == null) { + return res; + } + Stack stack = new Stack<>(); + stack.push(new Object[]{root, false}); + + while (!stack.empty()) { + TreeNode node = (TreeNode) stack.peek()[0]; + Boolean visited = (Boolean) stack.peek()[1]; + stack.pop(); + + if (visited) { + res.add(node.val); + } else { // 先序遍历:中左右,入栈顺序相反 + if (node.right != null) { + stack.push(new Object[]{node.right, false}); + } + if (node.left != null) { + stack.push(new Object[]{node.left, false}); + } + stack.push(new Object[]{node, true}); + } + } + return res; + } +} +``` + +迭代法中序遍历(空指针标记法)代码如下: + ```java class Solution { public List inorderTraversal(TreeNode root) { @@ -281,7 +316,41 @@ public List inorderTraversal(TreeNode root) { } ``` -迭代法后序遍历代码如下: +迭代法中序遍历(boolean标记法)代码如下: + +```java +class Solution { + public List inorderTraversal(TreeNode root) { + List res = new LinkedList<>(); + if (root == null) { + return res; + } + Stack stack = new Stack<>(); + stack.push(new Object[]{root, false}); + while (!stack.empty()) { + TreeNode node = (TreeNode) stack.peek()[0]; + Boolean visited = (Boolean) stack.peek()[1]; + stack.pop(); + + if (visited) { + res.add(node.val); + } else { // 中序遍历:左中右,入栈顺序相反 + if (node.right != null) { + stack.push(new Object[]{node.right, false}); + } + stack.push(new Object[]{node, true}); + if (node.left != null) { + stack.push(new Object[]{node.left, false}); + } + } + } + return res; + } +} +``` + +迭代法后序遍历(空指针标记法)代码如下: + ```java class Solution { public List postorderTraversal(TreeNode root) { @@ -309,6 +378,40 @@ class Solution { } ``` +迭代法后序遍历(boolean标记法)代码如下: + +```java +class Solution { + public List postorderTraversal(TreeNode root) { + List res = new LinkedList<>(); + if (root == null) { + return res; + } + Stack stack = new Stack<>(); + stack.push(new Object[]{root, false}); + + while (!stack.empty()) { + TreeNode node = (TreeNode) stack.peek()[0]; + Boolean visited = (Boolean) stack.peek()[1]; + stack.pop(); + + if (visited) { + res.add(node.val); + } else { // 后序遍历:左右中,入栈顺序相反 + stack.push(new Object[]{node, true}); + if (node.right != null) { + stack.push(new Object[]{node.right, false}); + } + if (node.left != null) { + stack.push(new Object[]{node.left, false}); + } + } + } + return res; + } +} +``` + ### Python: > 迭代法前序遍历(空指针标记法):