Java inorder traversal solution

  • 0

    Uses an array to kind of hackish pass integers by reference.

    public class Solution {
        public int kthSmallest(TreeNode root, int k) {
            int[] ctr = new int[2];
            ctr[0] = k;
            helper(root, k, ctr);
            return ctr[1];
        private void helper(TreeNode n, int k, int[] ctr)
            if (n.left != null)
                helper(n.left, k, ctr);
            if (ctr[0] == 0) 
                ctr[1] = n.val;
            if (n.right != null)
                helper(n.right, k, ctr);

Log in to reply

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