资讯专栏INFORMATION COLUMN

leetcode112 path sum

Lsnsh / 1744人阅读

摘要:题目要求假设有一个二叉树,和一个目标值,如果存在一条从根节点到叶节点的路径,该路径上所有节点上的值的和恰好等于该目标值,则返回,否则返回方法的输入为根节点和目标值例如假设有一颗二叉树如下,目标值为,结果返回,因为存在一条路径其和为思路

题目要求

假设有一个二叉树,和一个目标值,如果存在一条从根节点到叶节点的路径,该路径上所有节点上的值的和恰好等于该目标值,则返回true,否则返回FALSE
方法的输入为根节点和目标值
例如:假设有一颗二叉树如下,目标值为22,结果返回true,因为存在一条路径5->4->11->2其和为22

              5
             / 
            4   8
           /   / 
          11  13  4
         /        
        7    2      1
思路一 : 递归
/**
 * @author rale
 *
 *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.
 */

public class PathSum_112 {
    public boolean hasPathSum(TreeNode root, int sum) {
        if(root==null){
            return false;
        }
        sum -= root.val;
        if(root.left==null && root.right==null && sum==0){
            return true;
        }
        return hasPathSum(root.left, sum) || hasPathSum(root.right, sum);
    }
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }
}
思路二:循环(使用堆栈)
public class PathSum_112 {    
    public boolean hasPathSum2(TreeNode root, int sum) {
        Stack  stack = new Stack<> ();        
        stack.push(root) ;        
        while (!stack.isEmpty() && root != null){
            TreeNode cur = stack.pop() ;    
            if (cur.left == null && cur.right == null){                
                if (cur.val == sum ) return true ;
            }
            if (cur.right != null) {
                cur.right.val = cur.val + cur.right.val ;
                stack.push(cur.right) ;
            }
            if (cur.left != null) {
                cur.left.val = cur.val + cur.left.val;
                stack.push(cur.left);
            }
        }        
        return false ;
    }
    
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }
}


想要了解更多开发技术,面试教程以及互联网公司内推,欢迎关注我的微信公众号!将会不定期的发放福利哦~

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/66907.html

相关文章

  • LeetCode 之 JavaScript 解答第112题 —— 路径总和(Path Sum

    摘要:小鹿题目路径总和给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和。说明叶子节点是指没有子节点的节点。 Time:2019/4/26Title: Path SumDifficulty: EasyAuthor: 小鹿 题目:Path Sum(路径总和) Given a binary tree and a sum, determin...

    lylwyy2016 评论0 收藏0
  • [LeetCode] Path Sum (I & II & III)

    摘要: 112. Path Sum Problem 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. Note: A leaf is a node...

    张金宝 评论0 收藏0
  • leetcode部分题目答案之JavaScript版

    摘要:自己没事刷的一些的题目,若有更好的解法,希望能够一起探讨项目地址 自己没事刷的一些LeetCode的题目,若有更好的解法,希望能够一起探讨 Number Problem Solution Difficulty 204 Count Primes JavaScript Easy 202 Happy Number JavaScript Easy 190 Reverse Bi...

    alphahans 评论0 收藏0
  • LeetCode 攻略 - 2019 年 7 月下半月汇总(100 题攻略)

    摘要:月下半旬攻略道题,目前已攻略题。目前简单难度攻略已经到题,所以后面会调整自己,在刷算法与数据结构的同时,攻略中等难度的题目。 Create by jsliang on 2019-07-30 16:15:37 Recently revised in 2019-07-30 17:04:20 7 月下半旬攻略 45 道题,目前已攻略 100 题。 一 目录 不折腾的前端,和咸鱼有什么区别...

    tain335 评论0 收藏0
  • 前端 | 每天一个 LeetCode

    摘要:在线网站地址我的微信公众号完整题目列表从年月日起,每天更新一题,顺序从易到难,目前已更新个题。这是项目地址欢迎一起交流学习。 这篇文章记录我练习的 LeetCode 题目,语言 JavaScript。 在线网站:https://cattle.w3fun.com GitHub 地址:https://github.com/swpuLeo/ca...我的微信公众号: showImg(htt...

    张汉庆 评论0 收藏0

发表评论

0条评论

Lsnsh

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<