{"id":3217,"date":"2018-07-19T00:53:28","date_gmt":"2018-07-19T07:53:28","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3217"},"modified":"2018-07-19T00:56:53","modified_gmt":"2018-07-19T07:56:53","slug":"leetcode-501-find-mode-in-binary-search-tree","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-501-find-mode-in-binary-search-tree\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 501. Find Mode in Binary Search Tree"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given a binary search tree (BST) with duplicates, find all the\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/Mode_(statistics)\" target=\"_blank\" rel=\"noopener\">mode(s)<\/a>\u00a0(the most frequently occurred element) in the given BST.<\/p>\n<p>Assume a BST is defined as follows:<\/p>\n<ul>\n<li>The left subtree of a node contains only nodes with keys\u00a0<b>less than or equal to<\/b>\u00a0the node&#8217;s key.<\/li>\n<li>The right subtree of a node contains only nodes with keys\u00a0<b>greater than or equal to<\/b>\u00a0the node&#8217;s key.<\/li>\n<li>Both the left and right subtrees must also be binary search trees.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>For example:<br \/>\nGiven BST\u00a0<code>[1,null,2,2]<\/code>,<\/p>\n<pre class=\"\">   1\r\n    \\\r\n     2\r\n    \/\r\n   2\r\n<\/pre>\n<p>return\u00a0<code>[2]<\/code>.<\/p>\n<p><b>Note:<\/b>\u00a0If a tree has more than one mode, you can return them in any order.<\/p>\n<p><b>Follow up:<\/b>\u00a0Could you do that without using any extra space? (Assume that the implicit stack space incurred due to recursion does not count).<\/p>\n<h1>Solution1: Recursion w\/ extra space<\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 8 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;int&gt; findMode(TreeNode* root) {            \r\n    inorder(root);    \r\n    return ans_;\r\n  }\r\nprivate:\r\n  int val_ = 0;\r\n  int count_ = 0;  \r\n  int max_count_ = 0;\r\n  vector&lt;int&gt; ans_;\r\n  \r\n  void inorder(TreeNode* root) {\r\n    if (root == nullptr) return;\r\n    inorder(root-&gt;left);\r\n    visit(root-&gt;val);\r\n    inorder(root-&gt;right);\r\n  }\r\n  \r\n  void visit(int val) {\r\n    if (count_ &gt; 0 &amp;&amp; val == val_) {\r\n      ++count_;   \r\n    } else {\r\n      val_ = val;\r\n      count_ = 1;\r\n    }\r\n    \r\n    if (count_ &gt; max_count_) {\r\n      max_count_ = count_;\r\n      ans_.clear();\r\n    }\r\n    \r\n    if (count_ == max_count_)\r\n      ans_.push_back(val);\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<h1><strong>Solution2: Recursion w\/o extra space<\/strong><\/h1>\n<p>Two passes. First pass to find the count of the mode, second pass to collect all the modes.<\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 8 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;int&gt; findMode(TreeNode* root) {            \r\n    inorder(root);    \r\n    mode_count_ = max_count_;    \r\n    count_ = 0;\r\n    inorder(root);\r\n    return ans_;\r\n  }\r\nprivate:\r\n  int val_ = 0;\r\n  int count_ = 0;\r\n  int mode_count_ = INT_MAX;\r\n  int max_count_ = 0;\r\n  vector&lt;int&gt; ans_;\r\n  \r\n  void inorder(TreeNode* root) {\r\n    if (root == nullptr) return;\r\n    inorder(root-&gt;left);\r\n    visit(root-&gt;val);\r\n    inorder(root-&gt;right);\r\n  }\r\n  \r\n  void visit(int val) {\r\n    if (count_ &gt; 0 &amp;&amp; val == val_) {\r\n      ++count_;      \r\n    } else {\r\n      val_ = val;\r\n      count_ = 1;\r\n    }\r\n    \r\n    if (count_ &gt; max_count_)\r\n      max_count_ = count_;\r\n    \r\n    if (count_ == mode_count_)\r\n      ans_.push_back(val);\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given a binary search tree (BST) with duplicates, find all the\u00a0mode(s)\u00a0(the most frequently occurred element) in the given BST. Assume a BST is defined&#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,342,92,28],"class_list":["post-3217","post","type-post","status-publish","format-standard","hentry","category-tree","tag-medium","tag-mode","tag-o1","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3217","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=3217"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3217\/revisions"}],"predecessor-version":[{"id":3219,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3217\/revisions\/3219"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3217"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3217"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}