simple, iterative & easy to understand -- no recursion


  • 0
    V
    public int FindSecondMinimumValue(TreeNode root) {
            int smin = -1;
            if(root == null) return smin;
            
            var t = root; var s = new Stack<TreeNode>();  s.Push(t);
            while(s.Count > 0){
                t = s.Pop();
                if(t.left == null && t.right == null) continue;
                if(t.left.val == t.right.val){
                    s.Push(t.right);
                    s.Push(t.left);
                }else{
                    var max = Math.Max(t.left.val, t.right.val);
                    smin = smin == -1 ? max : max < smin ? max:smin;
                    s.Push(t.left.val == t.val ? t.left : t.right);
                }
            }
            return smin;
        }
    

Log in to reply
 

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