/**
* 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 help(TreeNode* root, int min_val) {
if(!root) return -1;
if(root->val > min_val) return root->val;
int left = help(root->left, min_val);
int right = help(root->right, min_val);
if(left == -1) return right;
if(right == -1) return left;
return min(left, right);
}
int findSecondMinimumValue(TreeNode* root) {
if(!root) return -1;
return help(root, root->val);
}
};
leetcode 671. 二叉树中第二小的节点
最新推荐文章于 2024-04-25 22:50:51 发布