Binary Tree Inorder Traversal Algorithm

The Binary Tree Inorder Traversal Algorithm is a depth-first traversal technique used to visit all the nodes in a binary tree in a specific order. In this algorithm, the nodes of the binary tree are visited in the following sequence: first, the left subtree is traversed, then the root node is visited, and finally, the right subtree is traversed. This sequence is applied recursively for each subtree in the binary tree. The result of the inorder traversal is a list of the tree's nodes sorted in ascending order, which makes this algorithm particularly useful when working with binary search trees. To implement the inorder traversal algorithm, one can use either a recursive or an iterative approach. In the recursive approach, the algorithm first calls itself for the left subtree of the root node, then it processes the root node by adding its value to the resulting list, and finally, it calls itself for the right subtree of the root node. The iterative approach, on the other hand, uses a stack data structure to keep track of the nodes yet to be visited. Starting with the root node, it pushes all the nodes in the left subtree onto the stack, then pops and processes the top node, and repeats the process for the right subtree. This iterative approach can be more efficient in terms of memory usage, as it eliminates the overhead of recursive function calls.
/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> inorderTraversal(TreeNode *root) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        
        vector<int> inorder;
        stack<TreeNode*> tree_stack;
        TreeNode *node = root;
        
        while (node || !tree_stack.empty()) {
            while (node) {
                tree_stack.push(node);
                node = node->left;
            }
            if (!tree_stack.empty()) {
                node = tree_stack.top();
                tree_stack.pop();
                inorder.push_back(node->val);
                node = node->right;
            }
        }
        return inorder;
    }
};

// solution 2, O(1) space
class Solution {
public:
    vector<int> inorderTraversal(TreeNode *root) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        
        vector<int> inorder;
        TreeNode *current = root;
        
        while (current != NULL) {
            if (current->left == NULL) {
                inorder.push_back(current->val);
                current = current->right;
            }
            else {
                TreeNode *prev = current->left;
                while (prev->right && prev->right != current)
                    prev = prev->right;
                if (prev->right == NULL) {
                    prev->right = current;
                    current = current->left;
                }
                else {
                    inorder.push_back(current->val);
                    prev->right = NULL;
                    current = current->right;
                }
            }
        }
        return inorder;
    }
};

LANGUAGE:

DARK MODE: