LeetCode_112. Path Sum

112. Path Sum

Easy

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.

package leetcode.easy;

public class PathSum {
	@org.junit.Test
	public void test() {
		int sum = 22;
		TreeNode tn11 = new TreeNode(5);
		TreeNode tn21 = new TreeNode(4);
		TreeNode tn22 = new TreeNode(8);
		TreeNode tn31 = new TreeNode(11);
		TreeNode tn33 = new TreeNode(13);
		TreeNode tn34 = new TreeNode(4);
		TreeNode tn41 = new TreeNode(7);
		TreeNode tn42 = new TreeNode(2);
		TreeNode tn46 = new TreeNode(1);
		tn11.left = tn21;
		tn11.right = tn22;
		tn21.left = tn31;
		tn21.right = null;
		tn22.left = tn33;
		tn22.right = tn34;
		tn31.left = tn41;
		tn31.right = tn42;
		tn33.left = null;
		tn33.right = null;
		tn34.left = null;
		tn34.right = tn46;
		tn41.left = null;
		tn41.right = null;
		tn42.left = null;
		tn42.right = null;
		tn46.left = null;
		tn46.right = null;
		System.out.println(hasPathSum(tn11, sum));
	}

	public boolean hasPathSum(TreeNode root, int sum) {
		if (null == root) {
			return false;
		} else if (null == root.left && null == root.right && 0 == sum - root.val) {
			return true;
		} else {
			return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
		}
	}
}

原文地址:https://www.cnblogs.com/denggelin/p/11619789.html

时间: 2024-07-30 12:50:38

LeetCode_112. Path Sum的相关文章

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:

Path Sum 解答

Question 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 t

LeetCode: Binary Tree Maximum Path Sum 解题报告

Binary Tree Maximum Path SumGiven 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 SOLUTION 1: 计算树的最长path有2种情况: 1. 通过根的path. (1)如果左子树从左树根到任何一个N

Binary Tree Maximum Path Sum 自底向上求解(重重重)

题目: 链接 解答: 自底向上求解.left_max right_max分别返回了左右子树的最大路径和,如果左右子树最大路径和小于0,那么返回零, 用这个最大路径和和根节点的值相加,来更新最大值,同时, 更新返回该树的最大路径值. 代码: class Solution { public: int max = INT_MIN; int maxPathSum(TreeNode *root) { if (root == NULL) return 0; search(root); return max;

【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] 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 andsum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 题意:给定一数,在树中找出所有路径和等于该数的情况.方

第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)

124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节点为结尾(包含它或者不包含)的最大值,有两种情况,分别来自左儿子和右儿子设为Vnow. 然后考虑经过这个节点的情况来更新最终答案.更新答案后返回Vnow供父节点继续更新. 代码很简单. 有一个类似的很有趣的题目,给定一个二叉树,选择一条路径,使得权值最大的和最小的相差最大.参考POJ3728 cla

[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] ] 分析: 这道题目算是Path Sum的延伸,只不过

Project Euler 83:Path sum: four ways 路径和:4个方向

Path sum: four ways NOTE: This problem is a significantly more challenging version of Problem 81. In the 5 by 5 matrix below, the minimal path sum from the top left to the bottom right, by moving left, right, up, and down, is indicated in bold red an