437. Path Sum III
原题:
解题:
思路1就是:以根节点开始遍历找到适合路径,以根节点的左孩子节点开始遍历,然后以根节点的右孩子节点开始遍历,不断循环,也就是以每个节点为起始遍历点
代码如下:
class Solution {
public:
int pathSum(TreeNode* root, int sum)
{
if(!root) return 0;
int count = getPath(root,0,sum) + pathSum(root->left,sum) + pathSum(root->right,sum);
return count;
}
int getPath(TreeNode* node, int target, int sum)
{
if(!node) return 0;
target += node->val;
return (target == sum) + getPath(node->left, target, sum) + getPath(node->right, target, sum);
}
};
以下代码是论坛里看到的,思路差不多,也是递归:
class Solution {
public:
int pathSum(TreeNode* root, int sum) {
path(root,0,sum);
return total;
}
int total;
void incrementTotal(){
this->total++; } void path(TreeNode* root,int sum, int target){
if(root != NULL){ checkpath(root,0,target);
path(root->left,0,target);
path(root->right,0,target);
}
}
void checkpath(TreeNode* root,int sum,int target){ if(root == NULL){
return;
}
int check = sum + root->val;
if(check == target){
incrementTotal();
}
checkpath(root->left,check,target);
checkpath(root->right,check,target); } };
437. Path Sum III的更多相关文章
- 47. leetcode 437. Path Sum III
437. Path Sum III You are given a binary tree in which each node contains an integer value. Find the ...
- 【leetcode】437. Path Sum III
problem 437. Path Sum III 参考 1. Leetcode_437. Path Sum III; 完
- 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 ...
- 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] 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 437 Path Sum III 路径和
相关问题:112 path sum /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNo ...
- 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 ...
- 【easy】437. Path Sum III 二叉树任意起始区间和
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
- 437. Path Sum III(路径可以任意点开始,任意点结束)
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
随机推荐
- Round544div3E(1133E)
一.题目链接 https://codeforces.com/problemset/problem/1133/E 二.思路 显然要使用dp,因为中间有部分人不会选取. 令$dp[i][j]$表示在前$i ...
- Android之socket多线程
一.添加权限 <uses-permission android:name="android.permission.INTERNET" /> 二.输入输出流 客户端和服务 ...
- Java - 33 Java Applet基础
Java Applet基础 applet是一种Java程序.它一般运行在支持Java的Web浏览器内.因为它有完整的Java API支持,所以applet是一个全功能的Java应用程序. 如下所示是独 ...
- restframwork框架
一 APIView: class PublishView(APIView): def get(self,request): publish_list=Publish.objects.all() ret ...
- day1作业(格式化输出)
练习:用户输入姓名.年龄.工作.爱好 ,然后打印成以下格式------------ info of Egon -----------Name : EgonAge : 22Sex : male ...
- Linux中使用python测试主机存活 Linux系统CentOS Linux release 7.3.1611 (Core) py版本Python 2.7.5
下面是最初的情况 #/usr/bin/env python # -*- coding: utf-8 -*- import os import time import subprocess import ...
- 54.纯 CSS 创作一副国际象棋
原文地址:https://segmentfault.com/a/1190000015310484 感想:棋盘是 CSS 画的,棋子是 unicode 字符. HTML code: <html&g ...
- <转载> bat 脚本基本语法 http://blog.csdn.net/bluedusk/article/details/1500629
bat 脚本基本语法 2007-01-25 10:31 常用命令 echo.@.call.pause.rem(小技巧:用::代替rem)是批处理文件最常用的几个命令,我们就从他们开始学起. = ...
- 高程三 DOM对象
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- 1. apache如何启动
进入apache安装目录/bin/底下,用命令:./apachectl start 启动