0ms java simple solution


  • 0
    V
    public class Solution {
        boolean notFound = true;
        int answer = 0;
        int _k;
        public int kthSmallest(TreeNode root, int k) {
            _k = k;
            kthSmallestRecurse(root);
            return answer;
        }
        
        private void kthSmallestRecurse(TreeNode node){
            if(node == null) return;
            if(notFound) kthSmallestRecurse(node.left);
            if(--_k == 0){
               answer = node.val;
               notFound = false;
            } 
            if(notFound) kthSmallestRecurse(node.right);
        }
    }

Log in to reply
 

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