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. 让你的 Python 代码优雅又地道

    示例代码和引用的语录都来自Raymond的演讲.这是我按我的理解整理出来的,希望你们理解起来跟我一样顺畅! 遍历一个范围内的数字 for i in [0, 1, 2, 3, 4, 5]: print  ...

  2. c++ demo

    #include "stdafx.h" #include <boost\version.hpp> #include <boost\config.hpp> # ...

  3. Selenium IDE使用

    基于版本Selenium IDE 3.2.2(注:该工具不常用,可以使用定位元素是否存在) Selenium IDE可以录制也很方便,当然录下来的经常回放不成功,需要自己调试就是了.它是只针对Web页 ...

  4. java中如何认定一个变量和方法

    对于变量,用变量名就可以认定.对于a ,你不能要求它既是int,又是double.这是错误的: 同理,对于方法,它的方法签名是:方法名+参数     通过签名,已经认定一个方法.你不能要求一个方法有多 ...

  5. [c][c++]按位操作

    因为有时候需要大量的标志位来判断当前状态等.使用太多的int,bool等会使得程序不“漂亮” 这时候需要“位”操作来解决 建立一个标志位 unsigned ; 在定义一些宏,如 #define CON ...

  6. OpenModelica 在特定目录下生成仿真结果文件

    OMEdit的仿真结果文件存放在:C:\Users\***\AppData\Local\Temp\OpenModelica\OMEdit 可以在仿真时设置仿真结果文件名,可使用用绝对路径或相对路径 使 ...

  7. Android主页Activity对多个Fragment实现不同的沉浸式标题(图片或者文字标题)

    提示:讲解的该例实现是 FragmentTabHost + Fragment 实现: 1.示例效果图: 2.场景需求: 如示例图所示,在首页实现轮播图的沉浸,而 “发现” 和“我的”页是标题的沉浸. ...

  8. 浅谈Nginx负载均衡与F5的区别

    前言 笔者最近在负责某集团网站时,同时用到了Nginx与F5,如图所示,负载均衡器F5作为处理外界请求的第一道“墙”,将请求分发到web服务器后,web服务器上的Nginx再进行处理,静态内容直接访问 ...

  9. x1c 2018 体验

    总结一下: 2018对比2017优点: 1屏幕完爆:HDR WHD镜面屏完爆 FHD 雾面屏(污+雾,所谓的油腻感),还有色彩!正红色第一次觉得这么好看.别人看得出来看不出来我不知道,至少我能看出来非 ...

  10. Lua和C++交互 学习记录之六:全局函数交互

    主要内容转载自:子龙山人博客(强烈建议去子龙山人博客完全学习一遍) 部分内容查阅自:<Lua 5.3  参考手册>中文版 译者 云风 制作 Kavcc vs2013+lua-5.3.3 1 ...