Easy To Understand java solution Using Inorder Traversal


  • 0
    H

    Using Inorder Traversal

    ""
    public int kthsmallest(TreeNode root, int k){
    Stack<TreeNode> stack = new Stack<TreeNode>();
    TreeNode p = root;
    int ans=0;
    while (!stack.empty()||p!=null){
    if(p!=null){
    stack.push(p);
    p=p.left;
    }
    else {
    TreeNode t = stack.pop();
    if (k==0){
    ans=t.val;
    }
    k--;
    p=t.right;
    }
    }
    return ans;
    }
    }
    ""


Log in to reply
 

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