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
             /             4   8
           /   /           11  13  4
         /  \    /         7    2  5   1

return

[
   [5,4,11,2],
   [5,8,4,5]
]

Hide Tags

Tree Depth-first Search

Have you met this question in a real interview?

Yes

No

Discuss

这道题采用深度优先搜索的算法来做,由于想记录下从根节点到每个节点的各个节点,所以在用堆栈的时候没有多加了一个向量来存储路径

#include<iostream>
#include<stack>
#include<utility>
#include<vector>
using namespace std;

struct TreeNode {
	     int val;
	     TreeNode *left;
	     TreeNode *right;
	     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
	 };

vector<vector<int> > pathSum(TreeNode *root, int sum)
{
	vector<vector<int> > result;
	if(root==NULL)
		return result;

	stack<pair<TreeNode*,vector<int> > > sta_temp;
	vector<int> temp;
	temp.push_back(root->val);
	sta_temp.push(make_pair(root,temp));

	vector<int> Vtemp_top;
	TreeNode* Ttemp_top;

	while(!sta_temp.empty())
	{
		Vtemp_top=sta_temp.top().second;
		Ttemp_top=sta_temp.top().first;

		sta_temp.pop();

		if(Ttemp_top->left==NULL&&Ttemp_top->right==NULL)
		{
			int sum_temp=0;
			for(int i=0;i!=Vtemp_top.size();i++)
			{
				sum_temp+=Vtemp_top[i];
			}
			if(sum_temp==sum)
				{
					result.push_back(Vtemp_top);
			    }
	     }
		if(Ttemp_top->left!=NULL)
		{
			Vtemp_top.push_back(Ttemp_top->left->val);
			sta_temp.push(make_pair(Ttemp_top->left,Vtemp_top));
			Vtemp_top.pop_back();
		}
		if(Ttemp_top->right!=NULL)
		{
			Vtemp_top.push_back(Ttemp_top->right->val);
			sta_temp.push(make_pair(Ttemp_top->right,Vtemp_top));
			//temp.pop_back();
		}
     }
	return result;
}

int main()
{
	TreeNode* root=(TreeNode*)malloc(sizeof(TreeNode));
	root->val=1;
	root->right=NULL;
	root->left=(TreeNode*)malloc(sizeof(TreeNode));
	root->left->val=2;
	root->left->left=NULL;
	root->left->right=NULL;

	vector<vector<int> > temp;
	temp=pathSum(root,1);
}

  

时间: 2025-01-20 02:17:34

leetcode_113题——Path Sum II(深度优先搜索)的相关文章

[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

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

leetcode 刷题之路 66 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] ] 给定一个二叉树和数字sum,输出二叉树中从根节点到

LeetCode之“树”:Path Sum &amp;&amp; Path Sum II

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

【LeetCode】113. Path Sum II 基于Java和C++的解法及分析

113. Path Sum II Total Accepted: 80509 Total Submissions: 284188 Difficulty: Medium 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

【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] ] 思路:与[Leetcode]Path Sum 不同

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] ] 给定一个二叉树和一个值,找出所有根到叶的路径和等于

LeetCode: Path Sum II [113]

[题目] 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] ] [题意] 判断二叉树中是否存在一条从根到

[LeetCode][JavaScript]Path Sum II

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] ] https://leetco