【LeetCode-面试算法经典-Java实现】【112-Path Sum(路径和)】

【112-Path Sum(路径和)】


【LeetCode-面试算法经典-Java实现】【所有题目目录索引】

原题

  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.

题目大意

  给定一棵二叉树和一个和,判断从树的根结点到叶子结点的所有结点的和是否等于给定的和,如果等于,就返回true,否则返回false。

解题思路

  对树进行遍历,并且使用回溯法进行求解。

代码实现

树结点类

public class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int x) { val = x; }
}

算法实现类

public class Solution {
    private boolean stop = false; // 判断是否已经找到答案

    public boolean hasPathSum(TreeNode root, int sum) {
        calculate(root, 0, sum);
        return stop;
    }

    /**
     * 计算根到叶子结点的和
     * @param node 当前处理的节点
     * @param cur 从根节点到当前结点之前的所有节点和
     * @param sum 要求的和
     */
    private void calculate(TreeNode node, int cur, int sum) {
        if (!stop && node != null) { // 还没有找到答案,并且要处理的节点不为空

            // 如果是叶节点,就检查从根到当前叶节点的和是否为sum,如果是就说明已经找到,改变stop
            if (node.left == null && node.right == null && (node.val + cur == sum) ) {
                stop = true;
            }

            // 如果是非叶节点,继续处理
            if (node.left != null) {
                calculate(node.left, cur + node.val, sum);
            }

            if (node.right != null) {
                calculate(node.right, cur + node.val, sum);
            }
        }
    }
}

评测结果

  点击图片,鼠标不释放,拖动一段位置,释放后在新的窗口中查看完整图片。

特别说明

欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47414069

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-05 04:40:44

【LeetCode-面试算法经典-Java实现】【112-Path Sum(路径和)】的相关文章

[LeetCode]题解(python):112 Path Sum

题目来源 https://leetcode.com/problems/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. 题意分析 Input: a binary tree, sum Output: True or False Con

LeetCode 112. Path Sum路径总和 (C++)

题目: 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

112 Path Sum 路径总和

给定一棵二叉树和一个总和,确定该树中是否存在根到叶的路径,这条路径的所有值相加等于给定的总和.例如:给定下面的二叉树和 总和 = 22,              5             / \            4   8           /   / \          11  13  4         /  \      \        7    2      1返回 true, 因为存在总和为 22 的根到叶的路径 5->4->11->2.详见:https://l

【LeetCode-面试算法经典-Java实现】【064-Minimum Path Sum(最小路径和)】

[064-Minimum Path Sum(最小路径和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either

【LeetCode-面试算法经典-Java实现】【120-Triangle(三角形)】

[120-Triangle(三角形)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4

【LeetCode-面试算法经典-Java实现】【129-Sum Root to Leaf Numbers(全部根到叶子结点组组成的数字相加)】

[129-Sum Root to Leaf Numbers(全部根到叶子结点组组成的数字相加)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represent

【LeetCode-面试算法经典-Java实现】【129-Sum Root to Leaf Numbers(所有根到叶子结点组组成的数字相加)】

[129-Sum Root to Leaf Numbers(所有根到叶子结点组组成的数字相加)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents

【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】

[104-Maximum Depth of Binary Tree(二叉树的最大深度)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 题目大意 给定

【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】

[139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, given s = "leetcode", di

【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