树中是否存在路径和为 sum leecode java

https://oj.leetcode.com/problems/path-sum/

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public boolean hasPathSum(TreeNode root, int sum) {
        if(root==null) return false;
        if(root.left==null&&root.right==null)
        {
            if(sum==root.val) return true;

        }

        return hasPathSum(root.left,sum-root.val)||hasPathSum(root.right,sum-root.val);

    }
}

  

树中是否存在路径和为 sum leecode java

时间: 2024-08-16 15:54:52

树中是否存在路径和为 sum leecode java的相关文章

数据结构实践——二叉树排序树中查找的路径

本文是[数据结构基础系列(8):查找]中的实践项目参考. [项目 - 二叉树排序树中查找的路径] 设计一个算法,输出在二叉排序中查找时查找某个关键字经过的路径. [参考解答] 专为本项目设计的算法体现在函数int SearchBST(-)和void SearchResult()中. #include <stdio.h> #include <malloc.h> #define MaxSize 100 typedef int KeyType; //定义关键字类型 typedef cha

LeetCode 437. Path Sum III(统计路径和等于sum的路径数量)

题意:统计路径和等于sum的路径数量. (1)节点值可正可负 (2)路径两端不一定是根结点或叶子结点 (3)路径一定是向下 分析:路径起点 (1)位于root(统计以root开头的和等于sum的路径数量) (2)位于root->left子树(递归) (3)位于root->right子树(递归) /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode

求二叉树的路径和(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. Note: A leaf is a node with no children. Example: Given the below binary tree and sum = 22, 1234567 5

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,

Combination Sum leetcode java

题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target) w

树的最大深度 leecode java

秒杀/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public int maxDepth(TreeNode root) { if(root==null) return 0; int h1=maxDepth(roo

insertion Sort List (链表的插入排序) leecode java

逻辑简单,代码难写,基础不劳,leecode写注释不能出现中文,太麻烦,我写了大量注释,链表问题最重要的就是你那个指针式干啥的 提交地址https://oj.leetcode.com/problems/insertion-sort-list/ /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; *

4 Sum leetcode java

题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: Elements in a quadruplet (a,b,c,d) must be in non-descending or

3 Sum leetcode java

题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The s