Simple and easy recursive java solution


  • 0

    /**

    • Definition for a binary tree node.
    • public class TreeNode {
    • int val;
      
    • TreeNode left;
      
    • TreeNode right;
      
    • TreeNode(int x) { val = x; }
      
    • }
      */
      public class Solution {
      List<Integer> lt = new ArrayList<Integer>();
      public int kthSmallest(TreeNode root, int k) {
      helper(root);
      return lt.get(k-1);
      }
      public void helper(TreeNode root)
      {
      if(root==null) return;
      helper(root.left);
      lt.add(root.val);
      helper(root.right);
      }
      }

Log in to reply
 

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