LeetCode Oj 112. Path Sum 解题报告

112. Path Sum

My Submissions

Question

Total Accepted: 91133 Total
Submissions: 295432 Difficulty: 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.

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.

Subscribe to see which companies asked this question

Show Tags

Show Similar Problems

Have you met this question in a real interview?

Yes

No

Discuss

显然深搜求解,简单高效。注意叶子节点的定义,当且仅当左右孩子都为空时,才是叶子节点。如果一个节点有一个孩子时,是不能作为leaf的。

我的AC代码

public class PathSum {
	static Boolean ok = false;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		TreeNode treeNode = new TreeNode(1);
		TreeNode t1 = new TreeNode(2);
		System.out.println(hasPathSum(treeNode, 1));
		System.out.println(hasPathSum(null, 0));
		treeNode.left = t1;
		System.out.println(hasPathSum(treeNode, 1));

	}

	public static boolean hasPathSum(TreeNode root, int sum) {
		if(root == null) return false;
		ok = false;
        dfs(root, sum, 0);
        return ok;
    }

	public static void dfs(TreeNode root, int sum, int s) {
		if(ok == true) return;
		if(root.left == null && root.right == null) {
			if (s + root.val == sum) {
				ok = true;
			}
			return;
		}

		if(root.left != null) dfs(root.left, sum, s + root.val);
		if(root.right != null) dfs(root.right, sum, s + root.val);
	}
}
时间: 2024-08-06 14:47:07

LeetCode Oj 112. Path Sum 解题报告的相关文章

【LeetCode】Minimum Path Sum 解题报告

[题目] 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 down or right at any point in time. [思路] 求从左上角到右下角的最小路径值,典型的动态规划

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

LeetCode: Path Sum 解题报告

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             / \        

LeetCode: Minimum Path Sum 解题报告

Minimum Path Sum 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 down or right at any point in time. SOLUTION 1: 相当基础

【LeetCode】Binary Tree Maximum Path Sum 解题报告

[题目] Given 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 Return 6. [解析] 题意:在二叉树中找一条路径,使得该路径的和最大.该路径可以从二叉树任何结点开始,也可以到任何结点结束. 思路:递归求一条经过root的最大路径,这条路径可能是:

【LeetCode】112 - 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 2 1 return true, as t

【LeetCode】1. Two Sum 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51471280 Subject 出处:https://leetcode.com/problems/two-sum/ Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input

【一天一道LeetCode】#113. Path Sum II

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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,

LeetCode --- 64. Minimum Path Sum

题目链接:Minimum Path Sum 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 down or right at any point in time. 这道题的要求是在m*n