{"id":79,"date":"2017-09-04T14:40:40","date_gmt":"2017-09-04T21:40:40","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=79"},"modified":"2018-04-19T08:37:22","modified_gmt":"2018-04-19T15:37:22","slug":"leetcode-671-second-minimum-node-in-a-binary-tree","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/leetcode\/leetcode-671-second-minimum-node-in-a-binary-tree\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 671. Second Minimum Node In a Binary Tree"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 671. Second Minimum Node In a Binary Tree - \u5237\u9898\u627e\u5de5\u4f5c EP34\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/QXvbRrK1zjY?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p>Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly\u00a0<code>two<\/code>\u00a0or\u00a0<code>zero<\/code>\u00a0sub-node. If the node has two sub-nodes, then this node&#8217;s value is the smaller value among its two sub-nodes.<\/p>\n<p>Given such a binary tree, you need to output the\u00a0<b>second minimum<\/b>\u00a0value in the set made of all the nodes&#8217; value in the whole tree.<\/p>\n<p>If no such second minimum value exists, output -1 instead.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"lang:default highlight:0 decode:true\">Input:\r\n\r\n    2\r\n   \/ \\\r\n  2   5\r\n     \/ \\\r\n    5   7\r\n\r\nOutput: 5\r\nExplanation: The smallest value is 2, the second smallest value is 5.<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"lang:default highlight:0 decode:true\">Input:\r\n    2\r\n   \/ \\\r\n  2   2\r\n\r\nOutput: -1\r\nExplanation: The smallest value is 2, but there isn't any second smallest value.<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Solution: <\/strong><\/p>\n<p>Time complexity O(n), Space complexity O(n)<\/p>\n<pre class=\"lang:c++ decode:true \">\/**\r\n * Definition for a binary tree node.\r\n * struct TreeNode {\r\n *     int val;\r\n *     TreeNode *left;\r\n *     TreeNode *right;\r\n *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}\r\n * };\r\n *\/\r\nclass Solution {\r\npublic:\r\n    int findSecondMinimumValue(TreeNode* root) {\r\n        if(!root) return -1;\r\n        \r\n        \/\/ return BFS(root);        \r\n        return DFS(root, root-&gt;val);\r\n    }\r\nprivate:    \r\n    \/\/ Time complexity: O(n)\r\n    \/\/ Space complexity: O(n)\r\n    int BFS(TreeNode* root) {\r\n        if(!root) return -1;\r\n        \r\n        \/\/ Smallest value\r\n        int s1 = root-&gt;val;\r\n        \/\/ Sencond smallest value\r\n        int s2 = INT_MAX;\r\n        bool found = false;\r\n        \r\n        deque&lt;TreeNode*&gt; q;\r\n        q.push_back(root);\r\n        \r\n        while(!q.empty()) {\r\n            TreeNode* node = q.front();\r\n            q.pop_front();\r\n            \r\n            \/\/ Keep updating second smallest value\r\n            if(node-&gt;val &gt; s1 &amp;&amp; node-&gt;val &lt; s2) {\r\n                s2 = node-&gt;val;\r\n                found = true;\r\n                \/\/ No need to add it's children into queue\r\n                continue; \r\n            }\r\n            \r\n            if(!node-&gt;left) continue;\r\n            q.push_back(node-&gt;left);\r\n            q.push_back(node-&gt;right);\r\n        }\r\n        \r\n        return found?s2:-1;\r\n    }\r\n    \r\n    \/\/ Return the second smallest number in the (sub-tree)\r\n    \/\/ s1 is the smallest value\r\n    int DFS(TreeNode* root, int s1) {\r\n        if(!root) return -1;\r\n        \r\n        \/\/ If root's value is already grater than s1, \r\n        \/\/ then all its children's value should be &gt;= s1.\r\n        \/\/ Thus root's value is the second smallest one.\r\n        \/\/ No need to visit the \r\n        if(root-&gt;val &gt; s1) return root-&gt;val;\r\n        \r\n        int sl = DFS(root-&gt;left, s1);\r\n        int sr = DFS(root-&gt;right, s1);\r\n        \r\n        if(sl == -1) return sr;\r\n        if(sr == -1) return sl;\r\n        \r\n        \/\/ Return the smaller one among two subtrees\r\n        return min(sl, sr);\r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly\u00a0two\u00a0or\u00a0zero\u00a0sub-node. If the node has&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,45],"tags":[28],"class_list":["post-79","post","type-post","status-publish","format-standard","hentry","category-leetcode","category-tree","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/79","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=79"}],"version-history":[{"count":10,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/79\/revisions"}],"predecessor-version":[{"id":2647,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/79\/revisions\/2647"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=79"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=79"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=79"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}