美文网首页
binary-tree-level-order-traversa

binary-tree-level-order-traversa

作者: DaiMorph | 来源:发表于2019-05-02 12:23 被阅读0次
    class Solution {
    public:
        vector<vector<int> > levelOrder(TreeNode *root) {
            queue<TreeNode*>q;
            vector<vector<int>>ans;
            if(!root)return ans;
            q.push(root);
            while(!q.empty())
            {
                int size=q.size();
                vector<int>temp;
                while(size--)
                {
                    TreeNode*top=q.front();
                    q.pop();
                    temp.push_back(top->val);
                    if(top->left)q.push(top->left);
                    if(top->right)q.push(top->right);
                }
                ans.push_back(temp);
            }
            return ans;
        }
    };
    

    相关文章

      网友评论

          本文标题:binary-tree-level-order-traversa

          本文链接:https://www.haomeiwen.com/subject/pqianqtx.html