# 2236. Root Equals Sum of Children 題目:<https://leetcode.com/problems/root-equals-sum-of-children/> 解法:依題目步驟直接解 Python3: ``` python 3 # 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: TreeNode) -> bool: return root.val == root.left.val + root.right.val if __name__ == '__main__': left = TreeNode(4) right = TreeNode(6) root = TreeNode(10, left, right) print(Solution().checkTree(root)) ``` C: ``` c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> //Definition for a binary tree node. struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; }; bool checkTree(struct TreeNode* root) { return root->val == root->left->val + root->right->val; }; int main() { struct TreeNode* a = (struct TreeNode*)malloc(sizeof(struct TreeNode)); a->val = 4; a->left = NULL; a->right = NULL; struct TreeNode* b = (struct TreeNode*)malloc(sizeof(struct TreeNode)); b->val = 6; b->left = NULL; b->right = NULL; struct TreeNode* root = (struct TreeNode*)malloc(sizeof(struct TreeNode)); root->val = 10; root->left = a; root->right = b; bool ans = checkTree(root); printf("%s\n", ans ? "True" : "False"); return 0; } ``` ###### tags: `leetcode` `binary tree`