java inorder solution


  • 0
    A
    public class Solution {
        
        int k;
        
        public int kthSmallest(TreeNode root, int k) {
            this.k = k;
            return (int) kthSmallest(root);
        }
        
        public long kthSmallest(TreeNode node) {
            if (node == null) return Long.MAX_VALUE;
            long left = kthSmallest(node.left);
            if (left != Long.MAX_VALUE) {
                return left;
            } else {
                if (k == 1) {
                    return node.val;
                }
                k--;
                return kthSmallest(node.right);
            }
        }
    }

Log in to reply
 

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