C++ reversed in-order traversal solution


  • 0
    class Solution {
    public:
        TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) {
            stack<TreeNode*>s;
            DFS(root, s);
            while(s.top() != p) s.pop();
            s.pop();
            return s.empty() ? NULL : s.top();
        }
        
        void DFS(TreeNode* root, stack<TreeNode*>& s){
            if(!root) return;
            DFS(root->right, s);
            s.push(root);
            DFS(root->left, s);
        }
    };
    

Log in to reply
 

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