112. Path Sum

题目:112. Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
For example:
Given the below binary tree and sum = 22,
5
/
4 8
/ /
11 13 4
/ \
7 2 1
return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.

1,递归
 public boolean hasPathSum(TreeNode root, int sum) {
        if(root == null){
            return false;
        }
        return dfs(root,0,sum);
     }
     
     private boolean dfs(TreeNode root, int curSum, int sum){
         if(root.left == null && root.right == null){
            return (curSum + root.val) == sum;
         }
         
         if(root.left != null && root.right != null){
            return dfs(root.left,curSum + root.val,sum) || dfs(root.right,curSum + root.val,sum); 
         }
         
         if(root.left != null && root.right == null){
            return dfs(root.left,curSum + root.val,sum);
         }
         
         return dfs(root.right,curSum + root.val,sum);
     }
2,利用后序遍历
思路:后序非递归遍历,栈顶节点到栈底节点就是栈顶的节点到树根的路径
public boolean hasPathSum(TreeNode root, int sum) {
        if(root == null){
            return false;
        }
        
        //根节点到栈顶节点的路径
        Stack<TreeNode> pathsStack = new Stack<TreeNode>();
        TreeNode node = root;
        TreeNode tempNode = null;
        boolean hasVisited = true;
        do{
            while(node != null){
              pathsStack.push(node);
              node = node.left;
            }
            
            tempNode = null;
            hasVisited = true;
            while(!pathsStack.empty() && hasVisited){
                node = pathsStack.peek();
                if(tempNode == node.right){
                    tempNode = pathsStack.pop();
                    //判断根节点到叶子节点的路径和
                    if(tempNode.left == null && tempNode.right == null){
                        int pathSum = tempNode.val;
                        for(TreeNode treeNode : pathsStack){
                            pathSum += treeNode.val;
                        }
                    
                        if(sum == pathSum){
                            return true;
                        }
                    }
                }else{
                    node = node.right;
                    hasVisited = false;
                }
            }
            
        }while(!pathsStack.empty());
        
        return false;
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,787评论 0 33
  • 对于初入职场的新人来说,有一些雷区千万不要踩。 一、要求上司兑现随口说出的承诺 小丁在一家公司做财务。财务部每个月...
    张若扬阅读 752评论 4 14
  • 昨天听一个音频,亚马逊的管理者说我今天有两类企业,一类是拥有持续创新能力做到蒸蒸日上的企业,成为“day ...
    杏坛小莲阅读 349评论 0 1
  • 软的像云的沙 从脚下蔓延到海里 带着些腥味的海风从身后吹来 细细的沙就拂过脚面 轻轻摩挲着 撩拨起我对你的欲念 一...
    i_nu阅读 155评论 0 0
  • 秋风见凉,孩子们穿上了厚厚的秋装,走进静谧的校园。 我站在楼上看风景,早到的孩子们,三三两两,说说笑笑。勤快的值日...
    心之山水阅读 532评论 0 3