Leetcode#113Path Sum II

Path Sum II

Total Accepted: 43473 Total Submissions: 162906My 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]
]

分析:遍历树中的每一条路经,查看是否满足条件,采用栈数据结构

public class Solution {

List<List<Integer>> x=new ArrayList<List<Integer>>();

Stack<TreeNode> stack=new Stack<TreeNode>();

void input(int s){

List<Integer> y=new ArrayList<Integer>();

Stack<TreeNode> mid=new Stack<TreeNode>();

while(!stack.isEmpty())

{

TreeNode z=stack.peek();

stack.pop();

mid.push(z);

}

int sum=0;

while(!mid.isEmpty())

{

TreeNode z=mid.peek();

sum=sum+z.val;

y.add(z.val);

mid.pop();

stack.push(z);

}

if(sum==s)

x.add(y);

}

public List<List<Integer>> pathSum(TreeNode root, int sum) {

if(root==null)

return x;

else

{

stack.push(root);

while(!stack.isEmpty())

{

TreeNode tn=stack.peek();

if(tn.left!=null)

{

stack.pop();

TreeNode mid=tn.left;

tn.left=null;

stack.push(tn);

stack.push(mid);

}

else if(tn.right!=null)

{

stack.pop();

TreeNode mid=tn.right;

tn.right=null;

stack.push(tn);

stack.push(mid);

}

else

{

input(sum);

while(!stack.isEmpty())

{

TreeNode v=stack.peek();

if(v.left==null&&v.right==null)

stack.pop();

else

break;

}

}

}

return x;

}

}

}

时间: 2024-08-03 11:21:04

Leetcode#113Path Sum II的相关文章

LeetCode: Combination Sum II [039]

[题目] Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be

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-面试算法经典-Java实现】【113-Path Sum II(路径和)】

[113-Path Sum II(路径和II)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 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

LeetCode: Combination Sum II 解题报告

Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note:All numbers (including ta

Leetcode#40Combination Sum II

Combination Sum II Total Accepted: 34820 Total Submissions: 138493My Submissions Question Solution Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each numb

LeetCode: 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] ] SOLUTION 1: 使用

【Leetcode】113Path 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] ] Tips:在112题的基础上,加深难度,本题要求输

[LeetCode] Two Sum II - Input array is sorted 两数之和之二 - 输入数组有序

Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the target, where index1 mu