<?php
class TreeNode
{
public $val = null;
public $left = null;
public $right = null;
function __construct($val = 0, $left = null, $right = null)
{
$this->val = $val;
$this->left = $left;
$this->right = $right;
}
}
class Solution
{
/**
* @param TreeNode $root
* @return Boolean
*/
function evaluateTree(TreeNode $root): bool
{
if ($root->left == null && $root->right == null) {
return $root->val == 1;
}
if ($root->val == 2) {
return $this->evaluateTree($root->left) || $this->evaluateTree($root->right);
}
return $this->evaluateTree($root->left) && $this->evaluateTree($root->right);
}
}
$root = new TreeNode(2);
$root->left = new TreeNode(1);
$root->right = new TreeNode(3);
$root->right->left = new TreeNode(0);
$root->right->right = new TreeNode(1);
$solution = new Solution();
var_dump($solution->evaluateTree($root));
力扣2331
最新推荐文章于 2024-11-04 12:20:00 发布