LeetCode112 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. (Easy)
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.
分析:
还是用递归的思路,可以把sum - root -> val用在递归函数中,这样写最简洁。
代码:
class Solution {
public:
bool hasPathSum(TreeNode* root, int sum) {
if(root == nullptr) {
return false;
}
if(root -> right == nullptr && root -> left == nullptr) {
return sum == root->val;
}
return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val) ;
}
};
LeetCode112 Path Sum的更多相关文章
- 第34-1题:LeetCode112. Path Sum I
题目 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 sum ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...
- [LeetCode] 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 ...
- [LeetCode] Path Sum 二叉树的路径和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- Path Sum II
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- Path Sum
需如下树节点求和 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1 JavaScript实现 window ...
随机推荐
- Python中的一些模块用法
python中os模块用法 python之模块之shutil模块 -------------------------------os模块-------------------------------- ...
- pc端拖拽
var move=document.getElementsByClassName("page1_2")[0]; var startX=0; var startY=0; var x= ...
- grpc入门2
rpc-gateway使用(同时提供rpc和http接口) 介绍第三方库 https://github.com/grpc-ecosystem/grpc-gateway 在grpc之上加一层代理并转发, ...
- 2019-9-2-win10-uwp-保存用户选择文件夹
title author date CreateTime categories win10 uwp 保存用户选择文件夹 lindexi 2019-09-02 12:57:38 +0800 2018-2 ...
- GYM100741 A Queries
A. Queries time limit per test 0.25 s memory limit per test 64 MB input standard input output standa ...
- 创建多个Django业务模块
manage.py startapp cmdb 如果运行错误记得加环境变量 c:\python35\python.exe manage.py startapp cmdb
- C++ operator new和new operator的区别
new operator 当你写这种代码: string *ps = new string("Memory Management"); 你使用的new是new operator. ...
- Hdu 3887树状数组+模拟栈
题目链接 Counting Offspring Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- js计算精度问题(浮点数误差,大数计算出错)
https://github.com/camsong/blog/issues/9 0.1+0.2 //0.30000000000000004 1-0.9 //0.09999999999999998 9 ...
- SVN经常使用操作
版权声明:本文为博主原创文章.转载请注明出处. https://blog.csdn.net/Jerome_s/article/details/27950055 Subversion(简称svn)安装 ...