352ms Java Solution with inorder traversal


  • -1
    A
    public class Solution {
        int num;
        public int kthSmallest(TreeNode root, int k) {
            this.num = k;
            return preFind(root);
        }
        
        public Integer preFind(TreeNode curr) {
            if (curr != null) {
                Integer temp = preFind(curr.left);
                if (temp != null) {
                    return temp;
                } 
                if (this.num > 1) {
                    this.num--;
                } else {
                    return curr.val;
                }
                temp = preFind(curr.right);
                if (temp != null) {
                    return temp;
                }
            }
            return null;
        }
    }

Log in to reply
 

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