Path Sum leetcode java

描述

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.

解析

递归解法

正常的树的递归操作。

非递归,使用队列

记录每条路径的值。

代码

递归解法

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
if (null == root) {
return false;
}
if (root.val == sum && root.left == null && root.right == null) {
return true;
}
boolean leftFlag = hasPathSum(root.left, sum - root.val);
boolean rightFlag = hasPathSum(root.right, sum - root.val);
return leftFlag || rightFlag;
}
}

非递归,使用队列

/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
if(root == null) return false; LinkedList<TreeNode> nodes = new LinkedList<TreeNode>();
LinkedList<Integer> values = new LinkedList<Integer>(); nodes.add(root);
values.add(root.val); while(!nodes.isEmpty()){
TreeNode curr = nodes.poll();
int sumValue = values.poll(); if(curr.left == null && curr.right == null && sumValue==sum){
return true;
} if(curr.left != null){
nodes.add(curr.left);
values.add(sumValue+curr.left.val);
} if(curr.right != null){
nodes.add(curr.right);
values.add(sumValue+curr.right.val);
}
} return false;
}
}

[LeetCode] 112. Path Sum ☆(二叉树是否有一条路径的sum等于给定的数)的更多相关文章

  1. (二叉树 DFS 递归) leetcode 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 ...

  2. LeetCode 112. Path Sum 二叉树的路径和 C++

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  3. [LeetCode] 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 ...

  4. leetcode 112. Path Sum 、 113. Path Sum II 、437. Path Sum III

    112. Path Sum 自己的一个错误写法: class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root ...

  5. [leetcode]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 ...

  6. LeetCode 112 Path Sum(路径和)(BT、DP)(*)

    翻译 给定一个二叉树root和一个和sum, 决定这个树是否存在一条从根到叶子的路径使得沿路全部节点的和等于给定的sum. 比如: 给定例如以下二叉树和sum=22. 5 / \ 4 8 / / \ ...

  7. LeetCode 112. Path Sum路径总和 (C++)

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  8. Leetcode 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 ...

  9. LeetCode 112. Path Sum(路径和是否可为sum)

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

随机推荐

  1. Java volatile 有什么作用

    在由Java语言编写的程序中.有时候为了提高程序的执行效率,编译器会自己主动对其进行优化,把经常被訪问的变量缓存起来,程序在读取这个变量的时候有可能会直接从缓存(比如寄存器)中来读取这个值.而不会去内 ...

  2. layui upload 后台获取不到值

    后台获取不到值方法一: <script> layui.use('upload', function () { var upload = layui.upload; //执行实例 var u ...

  3. Python 学习笔记 多进程 multiprocessing--转载

    本文链接地址 http://quqiuzhu.com/2016/python-multiprocessing/ Python 解释器有一个全局解释器锁(PIL),导致每个 Python 进程中最多同时 ...

  4. 宠物属性控制_pet

    classIndex 职业索引 DmgAddPct 根据职业的法伤或攻强来计算宠物增加的物理伤害,增加的伤害值等于玩家法伤或攻强的百分比 SpAddPct 根据职业的法伤或攻强来计算宠物增加的法术伤害 ...

  5. JaveWeb 公司项目(4)----- Easyui的表单验证

    前面三篇博文讲述的是界面的搭建和数据的传输,可以看出目前我做的这个小项目已经有了一个大体的雏形,剩下的就是细节部分的打磨和一些友好的人机交互设计,今天做的是表单的验证,作为初学者,着实花了一番功夫,所 ...

  6. Navicat for MySQL用ssh功能连接远程数据库

    转载自:http://holy2010.blog.51cto.com/1086044/518431 实现用本地的ssh隧道起到加密功能 在windows平台上运行Navicat for MySQL(h ...

  7. linux 打开一个文件现swap文件

    转自:http://blog.csdn.net/eckelwei/article/details/17078187 有时候在用vim打开文件时提示类似以下的信息: 发现交换文件 ".expo ...

  8. .NET扩展方法 封装公用方法

    定义方法的时候  第一个参数前面加上this   表示这个方法可以被IQueryable类型的对象.出来  调用的时候 只用传第二个参数  第一个参数不用传 第一个参数就是.出当前方法的参数 定义扩展 ...

  9. Centos 7系统挂载NTFS格式移动硬盘

    有些时候做大数据量迁移时,为了快速迁移大数据,有可能在Linux服务器上临时挂载NTFS格式的移动硬盘, 一般情况下,linux是识别不了NTFS格式移动硬盘的(需要重编译Linux核心才能,加挂NT ...

  10. Inception Network

    1. 下图为一个Inception 模块,即将输入的图像通过多种过滤器或者池化操作后,全部再给拼起来形成新的图像. 2. Inception 网络就是讲将多个Inception模块连起来而已,如下图的 ...