一.题目

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. HDU2874【倍增、ST】

    题目链接[https://vjudge.net/problem/HDU-2874] 题意: 输入一个森林,总节点不超过N(N<10000),由C次询问(C<1000000),每次询问两个点 ...

  2. 某DP题目1

    题意: 有n个由左右括号组成的字符串,选择其中若干字符串,使得组成的括号序列合法且长度最长.n <= 1000,n个字符串的长度和 <= 10000. 分析: 其实我一开始做这一题的时候, ...

  3. Apache之.htaccess备忘录(一)

    .htaccess文件是Apache服务器中的一个配置文件,它负责相关目录下的网页配置,也是使用apache的同学最常碰到的文件,下面罗列一些常用的知识,以备不时之需. 1 . 如何让Apache支持 ...

  4. python开发_filecmp

    filecmp模块用于比较文件及文件夹的内容,它是一个轻量级的工具,使用非常简单.python标准库还提供了difflib模块用于比较文件的内容.关于difflib模块,且听下回分解. filecmp ...

  5. zabbix install

    Auth: Jin Date: 20140714 用了5 6年的监控工具 http://zabbix.org/wiki/InstallOnCentOS_RHEL Server Install yum ...

  6. Linux下使用ISC DHCP可以实现动态推送静态路由表

    ISC DHCP可以实现动态推送静态路由表,先做个记号. 参考: https://gauvain.pocentek.net/docs/dhcpd-push-routes/ http://www.isc ...

  7. OC利用正则表达式获取网络资源(网络爬虫)

    在开发项目的过程,很多情况下我们需要利用互联网上的一些数据,在这种情况下,我们可能要写一个爬虫来爬我们所需要的数据.一般情况下都是利用正则表达式来匹配Html,获取我们所需要的数据.一般情况下分以下三 ...

  8. Process ID, Process handle, Window handle

    http://forums.codeguru.com/showthread.php?392273-RESOLVED-How-to-get-window-s-HWND-from-it-s-process ...

  9. NHibernate Configuring

    NHibernate引用程序中有几个关键组件,如下图所示: 初始化时,NHibernate应用程序将生成一个配置对象.本节中,我们通过设置App.config文件来生成该配置对象.该对象负责加载映射信 ...

  10. 【IntellJ IDEA】idea上所有代码都报错了

    可能会碰到蓝屏,内存溢出重启idea等特殊情况. 重新打开idea后发现原本的代码全都报错了 正确的解决方法: 方法很简单 执行idea工具栏上下面的菜单: File -> Invalidate ...