{"id":6785,"date":"2020-05-17T19:12:45","date_gmt":"2020-05-18T02:12:45","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6785"},"modified":"2020-05-17T19:13:25","modified_gmt":"2020-05-18T02:13:25","slug":"leetcode-1448-count-good-nodes-in-binary-tree","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-1448-count-good-nodes-in-binary-tree\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1448. Count Good Nodes in Binary Tree"},"content":{"rendered":"\n<p>Given a binary tree&nbsp;<code>root<\/code>, a node&nbsp;<em>X<\/em>&nbsp;in the tree is named&nbsp;<strong>good<\/strong>&nbsp;if in the path from root to&nbsp;<em>X<\/em>&nbsp;there are no nodes with a value&nbsp;<em>greater than<\/em>&nbsp;X.<\/p>\n\n\n\n<p>Return the number of&nbsp;<strong>good<\/strong>&nbsp;nodes in the binary tree.<\/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\/2020\/04\/02\/test_sample_1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root = [3,1,4,3,null,1,5]\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong> Nodes in blue are <strong>good<\/strong>.\nRoot Node (3) is always a good node.\nNode 4 -&gt; (3,4) is the maximum value in the path starting from the root.\nNode 5 -&gt; (3,4,5) is the maximum value in the path\nNode 3 -&gt; (3,1,3) is the maximum value in the path.<\/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\/2020\/04\/02\/test_sample_2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root = [3,3,null,4,2]\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> Node 2 -&gt; (3, 3, 2) is not good, because \"3\" is higher than it.<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root = [1]\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> Root is considered as <strong>good<\/strong>.<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The number of nodes in the binary tree is in the range&nbsp;<code>[1, 10^5]<\/code>.<\/li><li>Each node&#8217;s value is between&nbsp;<code>[-10^4, 10^4]<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Recursion<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/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  int goodNodes(TreeNode* root, int max_val = INT_MIN) {\n    if (!root) return 0;    \n    return goodNodes(root->left, max(max_val, root->val))\n           + goodNodes(root->right, max(max_val, root->val))\n           + (root->val >= max_val ? 1 : 0);\n  }\n};<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a binary tree&nbsp;root, a node&nbsp;X&nbsp;in the tree is named&nbsp;good&nbsp;if in the path from root to&nbsp;X&nbsp;there are no nodes with a value&nbsp;greater than&nbsp;X. Return the&#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":[177,17,28],"class_list":["post-6785","post","type-post","status-publish","format-standard","hentry","category-tree","tag-medium","tag-recursion","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6785","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=6785"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6785\/revisions"}],"predecessor-version":[{"id":6787,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6785\/revisions\/6787"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6785"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6785"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6785"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}