Second Minimum Node In a Binary Tree (671)
Approach 1 : DFS
class Solution {
public int findSecondMinimumValue(TreeNode root) {
Integer[]res = {root.val, null};
findMin(root, res);
return res[1] == null ? -1 : res[1];
}
private void findMin(TreeNode root, Integer[]res){
if(root == null) return;
findMin(root.left, res);
findMin(root.right, res);
if(root.val != res[0]){
res[1] = res[1] == null ? root.val : Math.min(res[1], root.val);
}
}
}Approach 2 : BFS
Last updated