*LeetCode 94. Binary Tree Inorder Traversal

来源:互联网 发布:js滑动到底部加载更多 编辑:程序博客网 时间:2024/05/15 23:47


https://leetcode.com/problems/binary-tree-inorder-traversal/

非递归中序遍历二叉树,基础啊。只是题意好久没看懂

struct Node {    TreeNode *tn;    bool vis;    Node (TreeNode *t, bool v):tn(t), vis(v) {}};class Solution {public:    vector<int> inorderTraversal(TreeNode* root) {        vector <int> ret;        if(root == NULL)            return ret;        stack < Node > sta;        sta.push( Node( root, false ) );        while( sta.size() ) {            while( !( sta.top().vis ) &&                    sta.top().tn->left ) {                sta.top().vis = true;                sta.push( Node(sta.top().tn->left, false ) );            }            ret.push_back( sta.top().tn->val );            TreeNode *tn = sta.top().tn;            sta.pop();            if( tn->right ) {                sta.push( Node(tn->right, false) );            }        }        return ret;    }};




0 0