leetCode(40):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
return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.
搜索支路的方法
/**
* 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 findPath(TreeNode* root, int nowSum,const int sum)
{
nowSum += root->val;//当前值 //假设已经是叶子结点,且和相等,则直接返回真
if (!root->left && !root->right && nowSum == sum)
return true; bool found_left = false;
bool found_right = false; if (root->left) //左子树不为空。则在左子树中搜索
found_left = findPath(root->left, nowSum, sum); if (!found_left && root->right) //假设左子树没找到,则在右子树中搜索
found_right = findPath(root->right, nowSum, sum); return found_left || found_right;//仅仅要在一条支中上找到则返回真
} bool hasPathSum(TreeNode* root, int sum) {
if (root == NULL)
return false;
findPath(root, 0, sum);
}
};
leetCode(40):Path Sum的更多相关文章
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- [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] 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] 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] 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 437. Path Sum III (路径之和之三)
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [leetcode]40. Combination Sum II组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)
LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...
随机推荐
- php 审批流程管理
1.流程管理的用法是什么样的? 2.怎么发起想要的流程? 3.审批的人要是怎么审批通过? 4.流程审核是不是要挨个走过? 一.要有数据库的内容的 肯定会有表的,首先就是用户表了,然后就是流程表,用户编 ...
- 换肤功能的实现以及监听storage实现多个标签页一起换肤
1:需求:项目的侧边栏实现换肤功能,核心代码: updateSkin (val) { const existSkinLink = document.head.querySelector('link[i ...
- Linux硬件资源管理与外设设备使用、系统运行机制及用户管理
Linux硬件资源管理 PCI设备 显卡 $>>dmesg |grep -i vga[ 0.000000] Console: colour VG ...
- jenkins项目数据位置
JENKINS_HOME: C:\Documents and Settings\AAA\.jenkins /root/.jenkins jenkins迁移只需要备份JENKINS_HOME的内容即可 ...
- Java-动态规划-最多苹果数量的方法
平面上有N*M个格子,每个格子中放着一定数量的苹果.你从左上角的格子开始,每一步只能向下走或是向右走,每次走到一个格子上就把格子里的苹果收集起来,这样下去,你最多能收集到多少个苹果. 思路: 解这个问 ...
- Using a USB host controller security extension for controlling changes in and auditing USB topology
Protecting computer systems from attacks that attempt to change USB topology and for ensuring that t ...
- ../wxs/utils.wxs not found from
../wxs/utils.wxs not found from 微信小程序,使用Vant Weapp时,引入到项目中时报以下错误: ... ../wxs/utils.wxs not found fro ...
- 升级 GCC 支持C++11 或 configure: error: *** A compiler with support for C++11 language features is required.
configure: error: *** A compiler with support for C++11 language features is required. 参考链接: (1)升级 G ...
- Linux 之 用户及用户组
用户及用户组 参考教程:[千峰教育] 命令: whoami: 作用:查看当前登录的用户. 格式:whoami /etc/passwd: 说明:该文件存放了系统中所有的用户,每一行的每一列如下: 用户名 ...
- python--导入就方便实现你需要的功能(模块)
模块让你能够有逻辑地组织你的Python代码段. 把相关的代码分配到一个 模块里能让你的代码更好用,更易懂. 模块也是Python对象,具有随机的名字属性用来绑定或引用. 简单地说,模块就是一个保存了 ...