LeetCode 671. Second Minimum Node In a Binary Tree(找出二叉树中第二小的节点)

题意:找出二叉树中第二小的节点。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int findSecondMinimumValue(TreeNode* root) {
        if(root == NULL) return -1;
        if(root -> left == NULL && root -> left == NULL) return -1;
        int l = root -> left -> val;
        int r = root -> right -> val;
        if(root -> val == root -> left -> val){
            l = findSecondMinimumValue(root -> left);
        }
        if(root -> val == root -> right -> val){
            r = findSecondMinimumValue(root -> right);
        }
        if(l == -1) return r;
        if(r == -1) return l;
        return min(l, r);
    }
};

  

原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/12517914.html