leetcode 94: Binary Tree Inorder Traversal

来源:互联网 发布:淘宝秒杀专区在哪 编辑:程序博客网 时间:2024/05/16 05:22
/** * 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<int> inorderTraversal(TreeNode* root) {        vector<int> res;        stack<TreeNode*> s;        TreeNode* curr=root;        while(curr)        {            s.push(curr);            curr=curr->left;        }        while(!s.empty())        {            res.push_back(s.top()->val);            curr=s.top()->right;            s.pop();            while(curr)            {                s.push(curr);                curr=curr->left;            }        }        return res;    }};

0 0
原创粉丝点击