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

判断二叉树是否存在一条从根节点到叶子节点的路径是的路径上节点的值之和等于给定的数sum,若存在返回true,否则返回false。

递归解法:每次递归sum都减去当前节点的值,当sum为0且当前节点为叶子节点时返回true;当sum不为0且当前节点为叶子节点时返回false;当sum不为0且当前

节点不为叶子节点时,根据当前节点的左右子树的情况,返回左子树的递归调用或右子树的递归调用或者是两者的或(||)。

代码如下:

 /**
* 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;
}
sum -= root->val;
if(sum == && root->left == NULL && root->right == NULL)
{
return true;
}
else if(root->left == NULL && root->right == NULL)
{
return false;
}
else if(root->left == NULL)
{
return hasPathSum(root->right, sum);
}
else if(root->right == NULL)
{
return hasPathSum(root->left, sum);
}
else
{
return hasPathSum(root->right, sum) || hasPathSum(root->left, sum);
}
}
};

leetcode 112的更多相关文章

  1. [LeetCode] #112 #113 #437 Path Sum Series

    首先要说明二叉树的问题就是用递归来做,基本没有其他方法,因为这数据结构基本只能用递归遍历,不要把事情想复杂了. #112 Path Sum 原题链接:https://leetcode.com/prob ...

  2. [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 ...

  3. LeetCode 112. 路径总和(Path Sum) 10

    112. 路径总和 112. Path Sum 题目描述 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节 ...

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

  5. leetcode 112. Path Sum 、 113. Path Sum II 、437. Path Sum III

    112. Path Sum 自己的一个错误写法: class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root ...

  6. [LeetCode] 112. Path Sum ☆(二叉树是否有一条路径的sum等于给定的数)

    Path Sum leetcode java 描述 Given a binary tree and a sum, determine if the tree has a root-to-leaf pa ...

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

  8. Java实现 LeetCode 112 路径总和

    112. 路径总和 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标 ...

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

随机推荐

  1. mongodb,redis,mysql 简要对比

    本篇内容大部分不是原创,转载的会贴有链接. 准备学习下数据库,想对目前的主流数据库做一个简单的了解分析,就搜集了资料整理到了一块. 当下主流的要数NoSql数据库了,拥有强大的高并发能力. mongo ...

  2. [物理学与PDEs]第2章习题参考解答

    [物理学与PDEs]第2章习题1 无旋时的 Euler 方程 [物理学与PDEs]第2章习题2 质量力有势时的能量方程 [物理学与PDEs]第2章习题3 Laplace 方程的 Neumann 问题 ...

  3. 30天轻松学习javaweb_模拟tomcat

    运行 javac Server.java 编译java文件 执行 java Server 运行程序 在ie中输入 http://localhost:9999/ 打开模拟的服务程序 import jav ...

  4. 查看linux的版本

    1. uname -a ~$ uname -a Linux lubuntu-Vostro-A840 3.19.0-73-generic #81-Ubuntu SMP Tue Oct 18 16:02: ...

  5. 查看mysql数据库引擎

    1.查看支持的引擎 show engines; 2.查看当前引擎 show variables like '%storage_engine%'

  6. java小程序 示例 菲薄垃圾数列

    package com.test; import java.util.Scanner; import org.junit.Test; import com.sun.xml.internal.ws.ap ...

  7. JAVA中集合输出的四种方式

    在JAVA中Collection输出有四种方式,分别如下: 一) Iterator输出. 该方式适用于Collection的所有子类. public class Hello { public stat ...

  8. C Primer Plus(第五版)4

    第四章 字符串和格式化输入输出 4.2 字符串简介 字符串(character string)就是一个或多个字符的序列.下面是一个字符串的例子: “Zing went the strings of m ...

  9. 论java虚拟类和接口的区别

    如题:Abstract使数据成员虚拟化,而Interface则使方法成员虚拟化.

  10. Android设置不被系统设置改变的字体大小

    原因 从4.0开始,系统设置中“显示”可以对字体大小进行配置,这会影响到TextView等控件中文字显示的大小. 解决方案 在自定义的Activity中重写getResources方法 @Overri ...