leetcode_112题——Path Sum(二叉树,深度优先搜索)

Path Sum

Total Accepted: 50593 Total Submissions: 169742My Submissions

Question Solution

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.

Hide Tags

Tree Depth-first Search

Have you met this question in a real interview?

Yes

No

Discuss

这道题需要查找出是否有路径等于给定值,采用深度优先搜索的方法,在每次往下搜索时,记录下到当前结点的路径,然后若到了叶节点时

就判断下是否与之相等,再将判断的结果回溯就可以了:

#include<iostream>
#include<stack>
#include<set>

using namespace std;
// Definition for binary tree
struct TreeNode
{
	int val;
    TreeNode *left;
	TreeNode *right;
	TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

bool ifsum(TreeNode* root,int sum,int cursum)
{
	if(root==NULL)
		return false;
	if(root->left==NULL&&root->right==NULL)
		return cursum+root->val==sum;

	if(root->left!=NULL||root->right!=NULL)
		return ifsum(root->left,sum,cursum+root->val)||ifsum(root->right,sum,cursum+root->val);
}
bool hasPathSum(TreeNode *root, int sum) {
	return ifsum(root,sum,0);
}
int main()
{

}

  还看到有一种基本与上面类似的方法,只是将当前的路径放在了外面作为迁居变量,而这里需要注意的是,在每次向下搜索完了之后,要记得在之前加了当前结点值的要减掉这个值

#include<iostream>
#include<stack>
#include<set>

using namespace std;
// Definition for binary tree
struct TreeNode
{
	int val;
    TreeNode *left;
	TreeNode *right;
	TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

int sum_temp;
int targetsum;
bool ifsum(TreeNode* root)
{
	if(root==NULL)
		return false;
	if(root->left==NULL&&root->right==NULL)
		return targetsum+root->val==sum_temp;

	targetsum+=root->val;
	bool res1=ifsum(root->left);
	bool res2=ifsum(root->right);

	targetsum-=root->val;//这里由于是全局变量,所以在前面加了之后,在这个结点算完后,要减掉

	return res1||res2;
}

bool hasPathSum(TreeNode *root, int sum) {
	sum_temp=sum;
	targetsum=0;
	return ifsum(root);
}
int main()
{

}

  

时间: 2024-10-09 18:55:35

leetcode_112题——Path Sum(二叉树,深度优先搜索)的相关文章

Leetcode:Path Sum 二叉树路径和

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

leetcode_113题——Path Sum II(深度优先搜索)

Path Sum II Total Accepted: 41402 Total Submissions: 155034My Submissions Question Solution Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example:Given the below binary tree and sum = 22, 5

LeetCode (12) 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, return true, as there exist a root-to-lea

[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 sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] Hide Tags Tree Depth-first

Leetcoede 112 Path Sum 二叉树

二叉树的从叶子到根的和是否存在 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 13 bool hasPathSum

Path Sum II深度优先找路径

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] Hide Tags Tree Depth-first

2019河北省大学生程序设计竞赛(重现赛) L题-smart robot(深度优先搜索)

题目链接:https://ac.nowcoder.com/acm/contest/903/L 题意:给你 n * n的方阵,你可以从任意一个数字开始走,可以走上下左右四个方向,走过的数字会被拼合,拼合的数字没有前导0,问最小不能拼合出来的数字是多少? 思路:开始就认为是一道深搜题,后面自己想太多就没去试,醉了orz! 这题就是把每个数字作为起点开始搜索,把已经搜索过的数字进行标记,然后从0开始检索,输出第一个未出现的数字就是答案.搜索的话我代码是搜5位数的所有组合,事实上这道题搜索3位数的所有组

leetcode笔记: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 tr

LeetCode_Binary Tree Maximum Path Sum

一.题目 Binary Tree Maximum Path Sum Total Accepted: 41576 Total Submissions: 193606My Submissions Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / 2 3 Retu