博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] Path Sum (I & II & III)
阅读量:6675 次
发布时间:2019-06-25

本文共 3757 字,大约阅读时间需要 12 分钟。

112. Path Sum

Problem

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.

Note: A leaf is a node with no children.

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.

Solution Recursive

class Solution {    public boolean hasPathSum(TreeNode root, int sum) {        if (root == null) return false;        //has to be a root-to-leaf path        if (root.val == sum && root.left == null && root.right == null) return true;        return hasPathSum(root.left, sum-root.val) || hasPathSum(root.right, sum-root.val);    }}

Update 2018-11

Solution Iterative

class Solution {    public boolean hasPathSum(TreeNode root, int sum) {        if (root == null) return false;        Deque
nodestack = new ArrayDeque<>(); Deque
sumstack = new ArrayDeque<>(); nodestack.push(root); sumstack.push(sum); while (!nodestack.isEmpty()) { TreeNode curNode = nodestack.pop(); int curSum = sumstack.pop(); if (curNode.left == null && curNode.right == null && curNode.val == curSum) return true; if (curNode.right != null) { nodestack.push(curNode.right); sumstack.push(curSum-curNode.val); } if (curNode.left != null) { nodestack.push(curNode.left); sumstack.push(curSum-curNode.val); } } return false; }}

Path Sum II

Problem

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.

Note: A leaf is a node with no children.

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

class Solution {    public List
> pathSum(TreeNode root, int sum) { List
> res = new ArrayList<>(); if (root == null) return res; dfs(root, sum, new ArrayList
(), res); return res; } private void dfs(TreeNode root, int sum, List
temp, List
> res) { if (sum == root.val && root.left == null && root.right == null) { temp.add(root.val); List
save = new ArrayList<>(temp); //important res.add(save); temp.remove(temp.size()-1); } else { temp.add(root.val); if (root.left != null) dfs(root.left, sum-root.val, temp, res); if (root.right != null) dfs(root.right, sum-root.val, temp, res); temp.remove(temp.size()-1); } }}

Path Sum III

Problem

You are given a binary tree in which each node contains an integer value.

Find the number of paths that sum to a given value.

The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes).

The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000.

Example:

root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8

10     /  \    5   -3   / \    \  3   2   11 / \   \3  -2   1

Return 3. The paths that sum to 8 are:

  1. 5 -> 3
  2. 5 -> 2 -> 1
  3. -3 -> 11

Solution

class Solution {    public int pathSum(TreeNode root, int sum) {        if (root == null) return 0;        return helper(root, sum) + pathSum(root.left, sum) + pathSum(root.right, sum);    }    private int helper(TreeNode root, int sum) {                int count = 0;        if (sum == root.val) count++;        if (root.left != null) count += helper(root.left, sum-root.val);        if (root.right != null) count += helper(root.right, sum-root.val);        return count;    }}

转载地址:http://pmgxo.baihongyu.com/

你可能感兴趣的文章
【语法】category
查看>>
实验一
查看>>
python 安装pip
查看>>
获得地址栏内的参数
查看>>
影响FPGA设计中时钟因素的探讨【转】
查看>>
make运行阶段划分
查看>>
cpio
查看>>
浅谈深度学习
查看>>
C++异常安全的赋值运算符重载 【微软面试100题 第五十五题】
查看>>
HDU 3535
查看>>
字符串指针
查看>>
ubuntu设置开机启动项
查看>>
进制与进制转换DAY2
查看>>
orcale 之 SQL 数据查询
查看>>
Unity引擎的Player Settings介绍
查看>>
Windows 更新的下载文件 保存位置
查看>>
[html] Doctype
查看>>
阅读笔记九
查看>>
求解形式幂级数的一阶微分方程
查看>>
life and penis
查看>>