Easy DFS solution in C#


  • 0
    K
    public class Solution {
        public int FindSecondMinimumValue(TreeNode root) {
            
            if(root == null) return -1;
            
            int min = root.val;
            int smin = root.val;
            return FindSMinValue(min, smin, root);
        }
        
        public int FindSMinValue(int min, int smin, TreeNode root)
        {
            
            if(root.left != null)
                smin = FindSMinValue(min, smin, root.left);
            
            if(root.val < min)
                min = root.val;
            
            if(smin == min && root.val > smin)
                smin = root.val;
            
            if(root.val > min &&  smin == -1)
            {
                smin = root.val;
            }
            else if(root.val > min && root.val < smin)
            {
                 smin = root.val;
            }
           
            if(root.right != null)
              smin =  FindSMinValue(min, smin, root.right);
                    
            if(min < smin)
                return smin;
            else 
                return -1;
        }
    }
    
    
    

Log in to reply
 

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