https://oj.leetcode.com/problems/path-sum/

树的深搜,求从根到叶子的路径。

记住深搜的样子

#include <iostream>
using namespace std;
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) {
//null
if(root == NULL )
return false; //leaf node
if(root->left == NULL && root->right == NULL && root->val == sum)
return true;
if(root->left == NULL && root->right == NULL)
return false; //not leaf node
if(root->left ||root->right)
{
bool ans = false; if(root->left)
{
ans = hasPathSum(root->left, sum - root->val);
if(ans == true)
return true;
}
if(root->right)
{
ans = hasPathSum(root->right, sum-root->val);
if(ans == true)
return true;
}
return false;
}
return false;
}
}; int main()
{
TreeNode *n1 = new TreeNode();
TreeNode *n2 = new TreeNode(-);
TreeNode *n3 = new TreeNode(-);
TreeNode *n4 = new TreeNode();
TreeNode *n5 = new TreeNode();
TreeNode *n6 = new TreeNode(-);
TreeNode *n7 = new TreeNode(-);
n1->left = n2;
n1->right = n3;
n2->left = n4;
n2->right = n5;
n3->left = n6;
n3->right = NULL;
n4->left = n7;
class Solution myS;
cout<<myS.hasPathSum(n1,);
return ;
}

LeetCode OJ--Path Sum *的更多相关文章

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

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

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

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

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

  7. [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)

    LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...

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

  9. 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance

    引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...

  10. [Leetcode Week14]Path Sum II

    Path Sum II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/path-sum-ii/description/ Description Giv ...

随机推荐

  1. matplotlib绘图股票走势图实践

    导入模块 import pandas as pdimport numpy as npfrom pandas import Series,DataFrameimport matplotlib.pyplo ...

  2. centos7 rpm安装mysql5.7

    1.去官网下载指定的数据库版本:https://dev.mysql.com/downloads/mysql/ 2.根据所用的操作系统下载指定的rpm包 3.下载及安装 地址链接wget https:/ ...

  3. 卸载firefox多余的搜索引擎

    火狐默认了几个搜索引擎,百度,bing,yahoo等.搜一些技术方面的东西的时候,google返回的结果比这些要准确有用.所以想卸载掉那些不用的. 具体操作: 点击搜索栏,左侧搜索引擎图票右下角的倒三 ...

  4. Centos7 install Openstack Juno (RDO) (转载)

    原文地址:http://www.hdume.com/centos-7-0%E5%AE%89%E8%A3%85openstack/ 1.安装系统,Centos7镜像采用CentOS-7.0-1406-x ...

  5. Root CA certificate:ApacheJMeterTemporaryRootCA.crt created in JMeter bin directory

    今天学习jmeter录制,在点击start之后弹出: 且在jmeter安装目录里确实生成了ApacheJMeterTemporaryRootCA.crt文件 上网查询官方文档http://120.52 ...

  6. webservice soap wsdl简介

    先给出一个概念 SOA ,即Service Oriented Architecture ,中文一般理解为面向服务的架构, 既然说是一种架构的话,所以一般认为 SOA 是包含了运行环境,编程模型, 架构 ...

  7. day08 多线程socket 编程,tcp粘包处理

    复习下socket 编程的步骤: 服务端:   1 声明socket 实例 server = socket.socket()  #括号里不写  默认地址簇使用AF_INET  即 IPv4       ...

  8. JDK并发基础与部分源码解读

    之前写的一个ppt 搬到博客来

  9. webdriver高级应用- 改变一个页面对象的属性值

    适用于一些无法操作的元素,可以直接改他的属性从而操作,代码如下: #encoding=utf-8 from selenium import webdriver import unittest impo ...

  10. 使用phppgadmin 遇到的小问题

    无法登录,显示错误消息如下: Error:login disallowed for security reasons. 解决方法: 修改conf/config.inc.php文件中的extra_log ...