当前位置: 面试刷题>> 二叉树的路径和Ⅰ (经典算法题500道)


题目描述补充

题目:二叉树的路径和Ⅰ

给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和。

说明:

  • 叶子节点是指没有子节点的节点。
  • 路径可以从任意节点开始,不一定从根节点开始。

示例: 给定如下二叉树和目标和 sum = 22

     5
    / \
   4   8
  /   / \
 11  13  4
/  \    / \
7    2  5   1

返回 true,因为存在一条路径 5->4->11->2,路径和等于 22。

PHP 示例代码

<?php

class TreeNode {
    public $val;
    public $left;
    public $right;
    
    function __construct($val = 0, $left = null, $right = null) {
        $this->val = $val;
        $this->left = $left;
        $this->right = $right;
    }
}

function hasPathSum($root, $sum) {
    if ($root === null) {
        return false;
    }
    
    // 如果当前节点是叶子节点且值等于目标和,则返回true
    if ($root->left === null && $root->right === null && $root->val === $sum) {
        return true;
    }
    
    // 递归检查左子树和右子树
    return hasPathSum($root->left, $sum - $root->val) || hasPathSum($root->right, $sum - $root->val);
}

// 示例用法
$root = new TreeNode(5);
$root->left = new TreeNode(4);
$root->right = new TreeNode(8);
$root->left->left = new TreeNode(11);
$root->left->left->left = new TreeNode(7);
$root->left->left->right = new TreeNode(2);
$root->right->left = new TreeNode(13);
$root->right->right = new TreeNode(4);
$root->right->right->left = new TreeNode(5);
$root->right->right->right = new TreeNode(1);

$sum = 22;
$result = hasPathSum($root, $sum);
echo $result ? "true" : "false";  // 输出 true

?>

Python 示例代码

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

def hasPathSum(root, sum):
    if not root:
        return False
    
    if not root.left and not root.right and root.val == sum:
        return True
    
    return hasPathSum(root.left, sum - root.val) or hasPathSum(root.right, sum - root.val)

# 示例用法
root = TreeNode(5)
root.left = TreeNode(4)
root.right = TreeNode(8)
root.left.left = TreeNode(11)
root.left.left.left = TreeNode(7)
root.left.left.right = TreeNode(2)
root.right.left = TreeNode(13)
root.right.right = TreeNode(4)
root.right.right.left = TreeNode(5)
root.right.right.right = TreeNode(1)

sum = 22
result = hasPathSum(root, sum)
print(result)  # 输出 True

JavaScript 示例代码

function TreeNode(val, left, right) {
    this.val = (val===undefined ? 0 : val)
    this.left = (left===undefined ? null : left)
    this.right = (right===undefined ? null : right)
}

function hasPathSum(root, sum) {
    if (!root) return false;
    
    if (!root.left && !root.right && root.val === sum) {
        return true;
    }
    
    return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
}

// 示例用法
let root = new TreeNode(5);
root.left = new TreeNode(4);
root.right = new TreeNode(8);
root.left.left = new TreeNode(11);
root.left.left.left = new TreeNode(7);
root.left.left.right = new TreeNode(2);
root.right.left = new TreeNode(13);
root.right.right = new TreeNode(4);
root.right.right.left = new TreeNode(5);
root.right.right.right = new TreeNode(1);

let sum = 22;
let result = hasPathSum(root, sum);
console.log(result); // 输出 true

// 码小课网站中有更多相关内容分享给大家学习
推荐面试题