{"id":4563,"date":"2018-12-30T12:07:33","date_gmt":"2018-12-30T20:07:33","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4563"},"modified":"2018-12-30T12:41:36","modified_gmt":"2018-12-30T20:41:36","slug":"leetcode-965-univalued-binary-tree","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-965-univalued-binary-tree\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 965. Univalued Binary Tree"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Problem<\/h2>\n\n\n\n<p>A binary tree is&nbsp;<em>univalued<\/em>&nbsp;if every node in the tree has the same value.<\/p>\n\n\n\n<p>Return&nbsp;<code>true<\/code>&nbsp;if and only if the given tree is univalued.<\/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\/2018\/12\/28\/unival_bst_1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>[1,1,1,1,1,null,1]\n<strong>Output: <\/strong>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\/2018\/12\/28\/unival_bst_2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>[2,2,2,5,2]\n<strong>Output: <\/strong>false\n<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>The number of nodes in the given tree will be in the range&nbsp;<code>[1, 100]<\/code>.<\/li><li>Each node&#8217;s value will be an integer in the range&nbsp;<code>[0, 99]<\/code>.<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution:\u00a0Recursion<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(h)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua, running time: 0 ms\nclass Solution {\npublic:\n  bool isUnivalTree(TreeNode* root) {\n    if (!root) return true;\n    if (root->left && root->val != root->left->val) return false;\n    if (root->right && root->val != root->right->val) return false;\n    return isUnivalTree(root->left) && isUnivalTree(root->right);\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua, running time: 68 ms\nclass Solution:\n  def isUnivalTree(self, root):\n    if not root: return True\n    if root.left and root.left.val != root.val: return False\n    if root.right and root.right.val != root.val: return False\n    return self.isUnivalTree(root.left) and self.isUnivalTree(root.right)\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Related Problems<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-687-longest-univalue-path\/\">https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-687-longest-univalue-path\/<\/a><\/li><li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-124-binary-tree-maximum-path-sum\/\">https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-124-binary-tree-maximum-path-sum\/<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem A binary tree is&nbsp;univalued&nbsp;if every node in the tree has the same value. Return&nbsp;true&nbsp;if and only if the given tree is univalued. Example 1:&#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,448],"class_list":["post-4563","post","type-post","status-publish","format-standard","hentry","category-tree","tag-easy","tag-tree","tag-univalue","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4563","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=4563"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4563\/revisions"}],"predecessor-version":[{"id":4571,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4563\/revisions\/4571"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}