My solution using inorder traversal


  • 0
    R
    public class Solution {
        public int kthSmallest(TreeNode root, int k) {
            Stack<TreeNode> stack = new Stack<>();
            ArrayList<Integer> arr = new ArrayList<>();
            TreeNode node = root;
            while(!stack.empty() || node != null){
                if(node != null){
                    stack.push(node);
                    node = node.left;
                }
                else{
                    node = stack.pop();
                    arr.add(node.val);
                    node = node.right;
                }
            }
            return arr.get(k-1);
        }
    }

Log in to reply
 

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