Java Solution


  • 0
    M
        int count;
        Integer kthSmallestEl;
    
        public int kthSmallest(TreeNode root, int k) {
            if (root == null) {
                return 0;
            } else if (root.left == null && root.right == null) {
                return root.val;
            }  
            
            kthSmallestUtil(root, k);
            return kthSmallestEl;
        }
        
        public void kthSmallestUtil(TreeNode root, int k) {
            
            if (root == null) {
                return;
            }
            
            kthSmallestUtil(root.left, k);        
            
            if (count == k) {
                return;
            } 
            
            kthSmallestEl = root.val;  
            count++; 
            
            kthSmallestUtil(root.right, k);
        }
    

Log in to reply
 

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