Binary Tree Inorder Traversal

 

CREATED TIME: Jan 21, 2020 4:19 PM UPDATED TIME: Jan 21, 2020 4:19 PM 난이도: Medium

/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: vector inorderTraversal(TreeNode* root) { vector res; stack<TreeNode*> st;

        TreeNode* node = root;
        
        while(node != NULL || !st.empty()){
            while(node != NULL){
                st.push(node);
                node = node -> left;
            }
            node = st.top();
            st.pop();
            res.push_back(node->val);
            node = node -> right;
        }
        return res;
    }
};