JAVA straight forward recursion solution


  • 1
    S
    public class Solution {
        int res = 0;
        int count = 0;
        public int kthSmallest(TreeNode root, int k) {
            count = k;
            helper(root);
            return res;
        }
        public void helper (TreeNode root) {
            if (root == null) return;
            helper(root.left);
            if (--count == 0) res = root.val;
            helper(root.right);
        }
    }

  • 1

    Is this gonna be any time problem? I think you always have to go through entire tree, no matter what k is.


  • 0
    B
    This post is deleted!

Log in to reply
 

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