Easy Solution using Stack


  • 0
    H
    public int kthSmallest( TreeNode root, int k ) {
    
    Stack<TreeNode> stack = new Stack<TreeNode>();
    int result=0;;
    TreeNode current = root;
    while(!stack.empty() || current !=null) {
        if(current !=null){
            stack.push(current);
            current = current.left;
        }
        else{
            TreeNode temp = stack.pop();
            k--;
            if(k==0) result = temp.val;
            current = temp.right;
            
        }
          
          
    }
    
    return result;
    

    }


Log in to reply
 

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