Short Java Recursive


  • 0
    Y
    public class Solution {
        public int kthSmallest(TreeNode root, int k) {
            
            List<Integer> l = new ArrayList<Integer>();
            list(root, l);        
            return l.get(k-1);
        }
        
        public void list(TreeNode node, List<Integer> l)
        {
            if(node == null)
                return;
            list(node.left, l);
            l.add(node.val);
            list(node.right, l);
        }
    }
    

Log in to reply
 

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