Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
532 B
1
Indexable
Never
    // Itterative Inorder traversal
    vector<int> inorderTraversal(TreeNode* root) {
        TreeNode * curr = root;
        stack<TreeNode *> st;
        vector<int> res;

        while (!st.empty() || curr != NULL) {
            if (curr != NULL) {
                st.push(curr);
                curr = curr -> left;
            } else {
                curr = st.top();
                st.pop();
                res.push_back(curr -> val);
                curr = curr -> right;
            }
        }
        return res;
    }