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. 动态拼接SQL 语句

    public T Get<T>(int id) { Type type = typeof(T); string columnStrings = string.Join(",&qu ...

  2. 【BZOJ】3575: [Hnoi2014]道路堵塞

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3575 大概的做法是,按照顺序枚举每一条要删去的边,(假设当前点为$u$,在最短路径上的下一 ...

  3. Python.错误解决:scrapy 没有crawl 命令

    确保2点: 1.把爬虫.py复制到spiders文件夹里 如执行scrapy crawl demo ,spiders里面就要有demo.py文件 2.在项目文件夹内执行命令 在scrapy.cfg所在 ...

  4. 力扣(LeetCode)258. 各位相加

    给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. 示例: 输入: 38 输出: 2 解释: 各位相加的过程为:3 + 8 = 11, 1 + 1 = 2. 由于 2 是一位数,所 ...

  5. 温馨小程序前端布局Flex

    伸缩容器支持的属性有: 1,display 2,flex-direction 3,flex-wrap 4,flex-flow 5,justify-content 6,align-items 7,ali ...

  6. 第 8 章 容器网络 - 059 - 安装配置 flannel

    安装配置 flannel 1) build flannel flannel 没有现成的执行文件可用,必须自己 build,最可靠的方法是在 Docker 容器中 build. 不过用于做 build ...

  7. Nginx自学笔记

    Nginx相关 标签(空格分隔): nginx 享学 安装部署 通过源代码的方式安装 使用 ./sbin/nginx #启动 ./sbin/nginx -t #检查是否有错 ./sbin/nginx ...

  8. (转)C# System.Diagnostics.Process.Start使用

    经常会遇到在Winform或是WPF中点击链接或按钮打开某个指定的网址, 或者是需要打开电脑中某个指定的硬盘分区及文件夹, 甚至是"控制面板"相关的东西, 如何做呢?  方法:使用 ...

  9. 雷林鹏分享:XML 树结构

    XML 树结构 XML 文档形成了一种树结构,它从"根部"开始,然后扩展到"枝叶". 一个 XML 文档实例 XML 文档使用简单的具有自我描述性的语法: To ...

  10. 用ActionController::Renderer的render方法渲染模版

    使用Cable进行pub: ActionCable.server.broadcast "call", {address: AddressesController.render(@a ...