#力扣:2236. 判断根结点是否等于子结点之和@FDDLC

2236. 判断根结点是否等于子结点之和

一、Java

java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean checkTree(TreeNode root) {
        return root.val == root.left.val + root.right.val;
    }
}

二、C++

cpp 复制代码
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool checkTree(TreeNode* root) {
        return root->val == root->left->val + root->right->val;
    }
};

三、Python

python 复制代码
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def checkTree(self, root: Optional[TreeNode]) -> bool:
        return root.val == root.left.val + root.right.val

四、JavaScript

javascript 复制代码
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
var checkTree = function(root) {
    return root.val == root.left.val + root.right.val;
};

五、Go

Go 复制代码
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func checkTree(root *TreeNode) bool {
    return root.Val == root.Left.Val + root.Right.Val
}
相关推荐
吃好睡好便好5 小时前
用if…end…语句计算分段函数
开发语言·人工智能·学习·算法·matlab
灰灰勇闯IT6 小时前
ops-memory:CANN Runtime 的 Tensor 内存管理
算法
叶子Talk6 小时前
OpenAI破解80年数学猜想,AI首次做出原创证明
人工智能·数学·算法·机器学习·ai·openai·ai推理
MhZhou04126 小时前
1.11M参数小模型实现脑瘤分割 CVPR 2026 Findings 开源
算法·计算机视觉·3d·空间计算
有为少年6 小时前
Welford算法 | 从单一到批次
大数据·人工智能·深度学习·神经网络·算法·机器学习
吴可可1236 小时前
Teigha处理CAD样条曲线的方法解析
数据库·算法·c#
啊董dong6 小时前
noi-2026年5月12号小测验
数据结构·c++·算法
不知名的忻6 小时前
红黑树(简易版)
算法·红黑树
NQBJT7 小时前
万字拆解 NeckFix:AI 脖子前倾检测的算法原理与工程实现
人工智能·算法
jaychouchannel7 小时前
Python 常用排序算法详解
算法