본문 바로가기
Problem Solving/LeetCode

[LeetCode] 671. Second Minimum Node In a Binary Tree - Java

by graycode 2026. 9. 20.

• 문제 링크

 

Second Minimum Node In a Binary Tree - LeetCode

Can you solve this real interview question? Second Minimum Node In a Binary Tree - Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two or zero sub-node. If the node has two sub

leetcode.com

 

• 풀이 코드

public class Solution {

    public int findSecondMinimumValue(TreeNode root) {
        if (root == null || root.left == null) return -1;
        int l = root.val == root.left.val ? findSecondMinimumValue(root.left) : root.left.val, r = root.val == root.right.val ? findSecondMinimumValue(root.right) : root.right.val;

        return l != -1 && r != -1 ? Math.min(l, r) : Math.max(l, r);
    }

}

댓글