C#
C++
Go
Java
Python
JavaScript
/**
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution
{
private static bool IsLeaf(TreeNode node)
{
return node.left == null && node.right == null;
}
public static bool HasTreePathSum(TreeNode root, int targetSum)
{
if (root == null)
{
return false;
}
targetSum -= root.val;
if (IsLeaf(root))
{
return targetSum == 0;
}
return HasTreePathSum(root.left, targetSum) || HasTreePathSum(root.right, targetSum);
}
}
#include <vector>
using namespace std;
/**
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* };
*/
bool isLeaf(TreeNode* node) {
return node->left == nullptr && node->right == nullptr;
}
bool hasTreePathSum(TreeNode* root, int targetSum) {
if (root == nullptr) {
return false;
}
targetSum -= root->val;
if (isLeaf(root)) {
return targetSum == 0;
}
return hasTreePathSum(root->left, targetSum) || hasTreePathSum(root->right, targetSum);
}
package main
// type TreeNode struct {
// Val int
// Left *TreeNode
// Right *TreeNode
// }
func isLeaf(node *TreeNode) bool {
return node.Left == nil && node.Right == nil
}
func hasTreePathSum(root *TreeNode, targetSum int) bool {
if root == nil {
return false
}
targetSum -= root.Val
if isLeaf(root) {
return targetSum == 0
}
return hasTreePathSum(root.Left, targetSum) || hasTreePathSum(root.Right, targetSum)
}
import java.util.*;
/**
* class TreeNode {
* Integer val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(Integer val) { this.val = val; }
* TreeNode(Integer val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
public boolean isLeaf(TreeNode node) {
return node.left == null && node.right == null;
}
public boolean hasTreePathSum(TreeNode root, int targetSum) {
if (root == null) {
return false;
}
targetSum -= root.val;
if (isLeaf(root)) {
return targetSum == 0;
}
return hasTreePathSum(root.left, targetSum) || hasTreePathSum(root.right, targetSum);
}
}
from typing import *
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
def is_leaf(node: TreeNode) -> bool:
return node.left is None and node.right is None
def has_tree_path_sum(root: Optional[TreeNode], target_sum: int) -> bool:
if root is None:
return False
target_sum -= root.val
if is_leaf(root):
return target_sum == 0
return has_tree_path_sum(root.left, target_sum) or has_tree_path_sum(root.right, target_sum)
from typing import *
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
def is_leaf(node: TreeNode) -> bool:
return node.left is None and node.right is None
def has_tree_path_sum(root: Optional[TreeNode], target_sum: int) -> bool:
if root is None:
return False
target_sum -= root.val
if is_leaf(root):
return target_sum == 0
return has_tree_path_sum(root.left, target_sum) or has_tree_path_sum(root.right, target_sum)