Leetcode Binary Tree&n…

来源:互联网 发布:南风知我意 书包网 编辑:程序博客网 时间:2024/06/07 23:09

    vector<<spanstyle="color:blue">int> preorderTraversal(TreeNode *root){

        vector<<spanstyle="color:blue">int> val_vector;

        std::stack<<spanstyle="color:blue">TreeNode*> my_stack;

 

        while(root||!my_stack.empty())

        {

            if(root){

                my_stack.push(root);

                val_vector.push_back(root->val);

                root=root->left;

            }

            else{

                root=my_stack.top();

                root=root->right;

                my_stack.pop();

            }

        }

        returnval_vector;

    }

0 0
原创粉丝点击