From 1a4a287243e43649166fea0d028dc8b733602387 Mon Sep 17 00:00:00 2001 From: nmydt <62681228+nmydt@users.noreply.github.com> Date: Thu, 3 Jun 2021 13:00:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0112.=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E6=80=BB=E5=92=8C=20JAVA=20=E8=BF=AD=E4=BB=A3=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0112.路径总和.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/problems/0112.路径总和.md b/problems/0112.路径总和.md index 4ccd8912..d810a046 100644 --- a/problems/0112.路径总和.md +++ b/problems/0112.路径总和.md @@ -345,6 +345,36 @@ class Solution { return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val); } } +``` +迭代 +```java +class Solution { + public boolean hasPathSum(TreeNode root, int targetSum) { + if(root==null)return false; + Stack stack1 = new Stack<>(); + Stack stack2 = new Stack<>(); + stack1.push(root);stack2.push(root.val); + while(!stack1.isEmpty()){ + int size = stack1.size(); + for(int i=0;i