一.题目

Path Sum

Total Accepted: 57762 Total Submissions: 193808My
Submissions

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.

Show Tags
Have you met this question in a real interview?

Yes
No

Discuss


























二.解题技巧

    这道题是一道深度优先搜索的。通过分别计算二叉树的左右子树是否的和是否等于sum-root->val来进行深度优先搜索。仅仅有到达也结点搜索才结束,因此,递归的退出条件就是到达叶结点,同一时候。也要考虑输入是空指针的情况,这种情况返回false值。同一时候,因为仅仅要推断是否存在。而不用找到每个这种路径。因此,仅仅要左子树满足条件时,就能够直接返回,不须要处理右子树,这样就能够进行剪枝,降低计算的复杂度。

    递归做法的时间复杂度为O(n),空间复杂度为O(logn)。


三.实现代码

#include <iostream>

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/ 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)
{
return false;
} if (!root->left && !root->right && root->val == sum)
{
return true;
} int SumChild = sum - root->val; if (hasPathSum(root->left, SumChild))
{
return true;
} if (hasPathSum(root->right, SumChild))
{
return true;
} return false;
}
};



四.体会

    这道题是一道普通的二叉树深度优先搜索的题。能够通过递归来实现。




版权全部,欢迎转载。转载请注明出处,谢谢




LeetCode_Path Sum的更多相关文章

  1. 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 ...

  2. LeetCode - Two Sum

    Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...

  3. 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 ...

  4. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  5. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  6. BZOJ 3944 Sum

    题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...

  7. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  8. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  9. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

随机推荐

  1. FastReport.Net使用:[5]主从表

    主从报表在日常生活中也很常用,譬如订单,班级学生报表等.下面以学生选课为例,以学生为主表,选课为从表做一学生选课报表. 绘制简单主从报表 1.绘制报表标题. 2.为数据区添加从表. ●可以直接右键数据 ...

  2. [CODE FESTIVAL 2018]Sushi Restaurant

    题意:有$n$个人,对每个人,他有$p_i$的概率饥饿值为$x_i$($1\leq i\leq m$),你现在要做$n$盘寿司,每盘寿司有一定的数量,当这$n$个人的饥饿值确定后他们会自己选择最优的( ...

  3. JS中地址栏参数的获取

    function getParamer(paramer) { var url = window.location.href.split("?")[1]; /* 获取url里&quo ...

  4. bzoj 2434 ac自动机

    ac自动机中,如果以trie中的节点为节点,(fail[i],i)为边,可以建立一颗树,该树有如下特点:“节点u是节点v的祖先 当且仅当 u代表的字符串是v代表的字符串的一个后缀”.(u代表的字符串是 ...

  5. php -- php读取sqlserver中的datetime出现的格式问题

    php连接sqlserver2005时,读取出来的数据是01 15 2014 12:00AM, 也就是说日期的格式是MM DD YY hh:mmAM 那如何把它转变成24小时制,且显示的格式为YY-M ...

  6. [转]Android中自定义checkbox样式

    android中自定义checkbox的图片和大小   其实很简单,分三步: 1.在drawable中创建文件checkbox_selector.xml: <?xml version=" ...

  7. 基于CDH,部署Apache Kylin读写分离

    一. 部署读写分离的契机 目前公司整体项目稳定运行在CDH5.6版本上,与其搭配的Hbase1.0.0无法正确运行Kylin,原因是Kylin只满足Hbase1.1.x+版本.解决方案如下 1. 升级 ...

  8. JProfiler 5.1.4的使用方法

    1. JProfiler运行环境配置 安装目录结构如下,子目录中显示了支持的操作系统: 在服务器和客户端都要安装JProfiler,并且要安装License,在分析工具客户端中进行安装.Windows ...

  9. 【转】2012年7月9 – 知名网页游戏公司 PHP高级工程师 最新面试题

    开头先唠叨两句,今天下午,上海的天热的让人窒息啊.Google下地图,好远!要做公交,想想就是人挤人.咬了下牙,打的,尼玛百来块啊,有木有!麻麻的,更让我萌生买车的决心了. 到了公司,环境不错.前台拿 ...

  10. 关于DIV多层嵌套的margin-top的BUG

    今天在做登录页面的时候发现个margin-top的bug ; 初始代码如下: html: <div class="logo"></div> <div ...