要求:根等于两孩子的最小值,且根只能有0或2个孩子
思路:明显往下是越来越大的
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int ans;
int val;
void dfs(TreeNode* root){
if(!root)return;
if(root->val>val&&(ans==-1||ans>root->val))ans=root->val;
dfs(root->left);
dfs(root->right);
}
int findSecondMinimumValue(TreeNode* root) {
val=root->val;
ans=-1;
dfs(root);
return ans;
}
};