LeetCode OJ:Path 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
要求求出是否有一条路径和与给出的值相等,注意中间节点与叶子节点的判断:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool hasPathSum(TreeNode* root, int sum) {
if(root == NULL) return false;
return checkSum(root, sum);
} bool checkSum(TreeNode* root, int sum)
{
if(root != NULL && sum == root->val && root->left == NULL && root->right == NULL){
return true;//上面这个判断确实是叶子节点,值也同时满足
}
else if(root == NULL)
return false;
else
return checkSum(root->left, sum - root->val) || checkSum(root->right, sum - root->val);
}
};
java版本的如下,递归版本的没上面那么麻烦:
public class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
if(root == null)
return false;
if(root.left == null && root.right == null){
return sum == root.val;
}
return hasPathSum(root.left, sum - root.val) ||
hasPathSum(root.right, sum - root.val);
}
}
LeetCode OJ:Path Sum(路径之和)的更多相关文章
- [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] Path sum路径之和
要求给定树,与路径和,判断是否存在从跟到叶子之和为给定值的路径.比如下图中,给定路径之和为22,存在路径<5,4,11,2>,因此返回true;否则返回false. 5 / \ 4 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 ...
- 【LeetCode】Path Sum(路径总和)
这道题是LeetCode里的第112道题.是我在学数据结构——二叉树的时候碰见的题.题目要求: 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和 ...
- 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] 666. Path Sum IV 二叉树的路径和 IV
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...
- [LeetCode] 113. Path Sum II 路径和 II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- [LeetCode] 437. Path Sum III 路径和 III
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] 437. Path Sum III_ Easy tag: DFS
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] 113. Path Sum II 二叉树路径之和之二
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
随机推荐
- 基于twemproxy的redis集群部署
一.系统及软件版本 操作系统:CentOS Linux release 7.3.1611 (Core) 内核版本:3.10.0-514.el7.x86_64 redis版本:3.2.8 twempro ...
- LVM逻辑卷管理器
LVM概述 通过使用Linux的逻辑卷管理器(Logical Volume Manager, LVM),用户可以在系统运行时动态调整文件系统的大小,把数据从一块硬盘重定位到另一块硬盘,也可以提高I/O ...
- 剑指offer 面试34题
面试34题: 题目:二叉树中和为某一值的路径 题:输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径. 解题代码 ...
- Traverse the dict in Python
We usually use the following 2 ways to traverse a dict: 1: for d in dic 2: for d in dic.keys() Which ...
- 调用settings.py的配置信息作为全局使用
项目中一些比较零散的信息可以保存在数据库,也可以保存在settings.py里面 并且这些变量也可以像引用数据里面的数据使用, 可以把信息保存在settings.py里面,也可以保存在数据 ...
- window + document + height
jQuery(window).height()代表了当前可见区域的大小,而jQuery(document).height()则代表了整个文档的高度,可视具体情况使用. 注意当浏览器窗口大小改变 ...
- freeMarker入门示例
1.创建Web项目freeMarkerDemo. 2.添加jar包---freemarker-2.3.9.jar. 3.在WebContent目录下新建templates文件夹,用于放置模板文件ftl ...
- win10系统修改Intel VT-x时进入不了BIOS问题
一般电脑进入BIOS的方式都是在开机的时候不停的按F2或者F12,但是Win10系统由于支持快速启动,当win10系统快速启动的时候,按F12或者F2是没反应的,解决方式: 第一步:修改win10的启 ...
- android开发之如何将一般应用变身系统级应用【转】
本文转载自:https://blog.csdn.net/zanelove/article/details/43953743 前提: ROOT过的手机 1,把代码编写好后,打包导出apk,copy到手机 ...
- Go panic recover
panic 1. 停止当前函数执行 2. 一直向上返回,执行每一层的defer 3. 如果没有遇到recover, 程序退出 recover 1. 仅在defer调用中使用 2. 获取panic的值 ...