MY simple solution


  • 0
    class Solution {
    public:
        vector<int> inorderTraversal(TreeNode* root) {
            if(!root)
                return vector<int>();
            vector<int>res;
            stack<TreeNode*>s;
            TreeNode*p=root;
            s.push(root);
            while(!s.empty()){
                if(p->left){
                    s.push(p->left);
                    p=p->left;
                }
                else{
                    if(!s.empty()){
                    while(1){
                        p=s.top();
                        s.pop();
                        res.push_back(p->val);
                        if(p->right||s.empty())//enter into right node or exit
                            break;
                     }
                     if(p->right){//enter into right node
                        p=p->right;
                        s.push(p);
                     }
                }
              }
                    
            }
            return res;
            
        }
    };

Log in to reply
 

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