From f22c658d365595a080ff5b516a3f2fca775e0ae6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=93=88=E5=93=88=E5=93=88?= <76643786+Projecthappy@users.noreply.github.com> Date: Wed, 31 May 2023 22:50:48 +0800 Subject: [PATCH] =?UTF-8?q?Update=200257.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E6=89=80=E6=9C=89=E8=B7=AF=E5=BE=84.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0257.二叉树的所有路径.md | 27 ++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/problems/0257.二叉树的所有路径.md b/problems/0257.二叉树的所有路径.md index 7bd56fbd..06153507 100644 --- a/problems/0257.二叉树的所有路径.md +++ b/problems/0257.二叉树的所有路径.md @@ -390,6 +390,8 @@ public: ```Java //解法一 + +//方式一 class Solution { /** * 递归法 @@ -428,9 +430,32 @@ class Solution { } } } + +//方式二 +class Solution { + + List result = new ArrayList<>(); + + public List binaryTreePaths(TreeNode root) { + deal(root, ""); + return result; + } + + public void deal(TreeNode node, String s) { + if (node == null) + return; + if (node.left == null && node.right == null) { + result.add(new StringBuilder(s).append(node.val).toString()); + return; + } + String tmp = new StringBuilder(s).append(node.val).append("->").toString(); + deal(node.left, tmp); + deal(node.right, tmp); + } +} ``` ```java -// 解法2 +// 解法二 class Solution { /** * 迭代法