C++ use inorder traversal non-recursive


  • -2
    L

    class Solution {
    /* use inorder traversal. when inorder traverse a bst, every node must be
    * larger than the previous one.
    /
    public:
    bool isValidBST(TreeNode
    root) {
    TreeNode *pre = NULL, cur = root;
    stack<TreeNode
    > st;
    while(cur || !st.empty()){
    while(cur){
    st.push(cur);
    cur = cur->left;
    }
    cur = st.top();
    st.pop();
    if(pre == NULL){
    pre = cur;
    }else{
    if(cur->val <= pre->val)
    return false;
    pre = cur;
    }

           cur = cur->right;
           
           
       }
       return true;
    }
    

    };


Log in to reply
 

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