Leetcode: Binary Tree Inorder Traversal

来源:互联网 发布:linux 查找文件夹大小 编辑:程序博客网 时间:2024/04/28 21:33
//the key is to use a stack smartlyclass Solution {public:    vector<int> inorderTraversal(TreeNode *root) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        //if(root==NULL)            //return NULL;        stack<TreeNode*> s;        vector<int> v;        v.clear();        TreeNode* np=root;        while(1){            while(np){                s.push(np);                np=np->left;            }            if(s.empty())                break;            TreeNode* tmp=s.top();            s.pop();            v.push_back(tmp->val);            np=tmp->right;        }        return v;           }

原创粉丝点击