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