C++, no additional function, DFS with stop condition

  • 0
    1. Each child's val >= parent's val.
      When we meet the condition "child's val > parent's val",
      we could stop the dfs path from this child.
    2. There is none negative value, using "unsigned int" to find the minimum val.
    class Solution {
        int findSecondMinimumValue(TreeNode* root) {
            if (!root->left) return -1;
            unsigned int l, r;
            if (root->left->val == root->val)
                l = findSecondMinimumValue(root->left);
                l = root->left->val;
            if (root->right->val == root->val)
                r = findSecondMinimumValue(root->right);
                r = root->right->val;
            return min(l, r);

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.