[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 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等于给定的数)的更多相关文章
- (二叉树 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 ...
- 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 ...
- [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 ...
- 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 ...
- [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 ...
- LeetCode 112 Path Sum(路径和)(BT、DP)(*)
翻译 给定一个二叉树root和一个和sum, 决定这个树是否存在一条从根到叶子的路径使得沿路全部节点的和等于给定的sum. 比如: 给定例如以下二叉树和sum=22. 5 / \ 4 8 / / \ ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- spring boot 配置双数据源mysql、sqlServer
背景:原来一直都是使用mysql数据库,在application.properties 中配置数据库信息 spring.datasource.url=jdbc:mysql://xxxx/test sp ...
- L1-048. 矩阵A乘以B
水题不多说,直接上代码:#include<stdio.h> using namespace std; int main() { ][]; ][]; int m,n; int x,y; sc ...
- GreenDao3使用完全解析
1,gradle配置(官网样例地址https://github.com/greenrobot/greenDAO/blob/master/examples/RxDaoExample/build.grad ...
- nRF52832定时器
1概述 定时器能够被配置为两种模式:定时模式和计数模式,nrf52832有五个定时器,timer0--timer4 . 2常用得函数 函数功能:初始化定时器 ret_code_t nrf_drv_ti ...
- eslint简单的规范
module.exports = { root: true, parser: 'babel-eslint', parserOptions: { sourceType: 'module' }, // h ...
- Day2-异步IO+Scrapy爬虫
一.异步IO http://www.cnblogs.com/wupeiqi/articles/6229292.html 这篇文章写的不错,展示了多种高并发的方式,从同步执行→多线程→多进程→async ...
- POSTMAN模拟http请求
附加小知识: chrome浏览器fitler中的XHR作用是什么? 记录ajax中的请求. AJAX :异步 JavaScript 和 XML 1.是一种用于创建快速动态网页的技术. 2. 通过在后台 ...
- python如何判断一个字符串是中文,还是英文。
参考链接: https://blog.csdn.net/hit0803107/article/details/52885702 decode: 将其它编码转成 ===>unicode enc ...
- Python 编程快速上手 第十五章 保持时间,计划任务和启动程序
前言 这一章节的主要内容是: 处理时间类型的数据(使用python 的两个模块: time 和 datetime 来处理) 创建多个线程 (使用 threading 模块来创建多个线程) 进行多个进程 ...
- Windows Phone 8 开发必备资源
一.MVVM框架推荐 1. MVVM-Light 这个框架是我最常用的MVVM框架之一,它比Prism更轻量级,但对于一般的小应用,功能足够. 官方网站:http://mvvmlight.codepl ...