LeetCode题解之Second Minimum Node In a Binary Tree

1、题目描述

 

2、问题分析

使用set。

 

3、代码

 1 set<int> s;
 2     int findSecondMinimumValue(TreeNode* root) {
 3         dfs(root);
 4         vector<int> v;
 5         for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
 6             v.push_back(*it);
 7         }
 8         
 9         sort(v.begin(), v.end());
10         return v.size() > 1 ? v[1] : -1;
11     }
12     
13     void dfs(TreeNode *root)
14     {
15         if (root == NULL)
16             return ;
17         s.insert(root->val);
18         dfs(root->left);
19         dfs(root->right);
20     }

 

posted @ 2019-02-28 21:39  山里的小勇子  阅读(114)  评论(0编辑  收藏  举报