#力扣: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
}
相关推荐
三行数学4 分钟前
数学周刊第23期(2026年06月08日-06月14日)南师数科院万仁辉副教授成果登顶国际数学四大顶刊之一<数学年刊>
算法·数学周刊
阿文的代码库6 分钟前
算法专题:独特的电子邮件地址
linux·运维·算法
老饼讲解-BP神经网络10 分钟前
BP神经网络用什么训练算法(traingd、traingdm、trainlm)
人工智能·神经网络·算法
Irissgwe11 分钟前
数据结构-二叉树
数据结构·c++·二叉树·c·
山峰哥15 分钟前
VBA数据结构之争:Dictionary vs Collection,性能差3倍!
服务器·数据结构·数据库·windows·sql·算法·哈希算法
兰令水2 小时前
leecodecode【面试150】【2026.6.14打卡-java版本】
java·算法·面试
noipp9 小时前
推荐题目:洛谷 P10907 [蓝桥杯 2024 国 B] 蚂蚁开会
c语言·c++·算法·编程·洛谷
程序员二叉10 小时前
【JUC】线程池全套深度详解|参数|流程|拒绝策略|调优|异常处理
java·开发语言·jvm·算法·面试·juc
青山木10 小时前
Hot 100 --- 轮转数组
java·数据结构·算法
徐小夕10 小时前
Loop Engineering 深度解析与实战指南(全网最全)
前端·算法·github