Summary of LeetCode solutions 2236. Determine whether the root node is equal to the sum of child nodes

Directory link:

Likou Programming Questions-Solution Summary_Sharing + Recording-CSDN Blog

GitHub synchronization problem solving project:

https://github.com/September26/java-algorithms

Original title link: LeetCode official website - the technology growth platform loved by geeks around the world


describe:

You are given  the root node of  a binary tree root . The binary tree  3 consists of exactly three nodes: the root node, the left child node, and the right child node.

If the root node value is equal to the sum of the two child node values, return it  true , otherwise return it  . false

Example 1:

Input: root = [10,4,6]
 Output: true
 Explanation: The values ​​of the root node, left subnode and right subnode are 10, 4 and 6 respectively.
Since 10 is equal to 4 + 6, it returns true.

Example 2:

Input: root = [5,3,1]
 Output: false
 Explanation: The values ​​of the root node, left subnode and right subnode are 5, 3 and 1 respectively.
Since 5 is not equal to 3 + 1, false is returned.

hint:

  • The tree only contains the root node, left child node and right child node
  • -100 <= Node.val <= 100

Problem-solving ideas:

Too simple, omit

Code:

class Solution {
public:
    bool checkTree(TreeNode* root) {
        return (root->left->val+root->right->val) == root->val;
    }
};

Guess you like

Origin blog.csdn.net/AA5279AA/article/details/132445066