{"id":9647,"date":"2022-04-16T08:09:51","date_gmt":"2022-04-16T15:09:51","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9647"},"modified":"2022-04-16T08:10:20","modified_gmt":"2022-04-16T15:10:20","slug":"leetcode-2236-root-equals-sum-of-children","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-2236-root-equals-sum-of-children\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2236. Root Equals Sum of Children"},"content":{"rendered":"\n<p>You are given the&nbsp;<code>root<\/code>&nbsp;of a&nbsp;<strong>binary tree<\/strong>&nbsp;that consists of exactly&nbsp;<code>3<\/code>&nbsp;nodes: the root, its left child, and its right child.<\/p>\n\n\n\n<p>Return&nbsp;<code>true<\/code>&nbsp;<em>if the value of the root is equal to the&nbsp;<strong>sum<\/strong>&nbsp;of the values of its two children, or&nbsp;<\/em><code>false<\/code><em>&nbsp;otherwise<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2022\/04\/08\/graph3drawio.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root = [10,4,6]\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> The values of the root, its left child, and its right child are 10, 4, and 6, respectively.\n10 is equal to 4 + 6, so we return true.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2022\/04\/08\/graph3drawio-1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root = [5,3,1]\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong> The values of the root, its left child, and its right child are 5, 3, and 1, respectively.\n5 is not equal to 3 + 1, so we return false.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The tree consists only of the root, its left child, and its right child.<\/li><li><code>-100 &lt;= Node.val &lt;= 100<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: <\/strong><\/h2>\n\n\n\n<p>Just want to check whether you know binary tree or not.<\/p>\n\n\n\n<p>Time complexity: O(1)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  bool checkTree(TreeNode* root) {\n    return root->val == root->left->val + root->right->val;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given the&nbsp;root&nbsp;of a&nbsp;binary tree&nbsp;that consists of exactly&nbsp;3&nbsp;nodes: the root, its left child, and its right child. Return&nbsp;true&nbsp;if the value of the root is&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[222,28],"class_list":["post-9647","post","type-post","status-publish","format-standard","hentry","category-tree","tag-easy","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9647","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9647"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9647\/revisions"}],"predecessor-version":[{"id":9650,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9647\/revisions\/9650"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9647"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9647"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9647"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}