0ms beat 95% Java Solution O(1) space in-order


  • 0
    D
    public class Solution {
        
        int counter = 0, k;
        int rt = 0;
        
        public void recur( TreeNode root ) {
            if ( counter == k || root == null )
                return;
                
            if ( root.left != null )
                recur( root.left );
            
            if ( counter != k ) {
                rt = root.val;
                counter++;
            }
            
            if ( root.right != null )
                recur( root.right );
        }
        
        public int kthSmallest(TreeNode root, int k) {
            
            this.k = k;
            recur( root );
            
            return rt;
        }
    }

Log in to reply
 

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