My solution using C++, any advise?


  • 4
    L
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> res;
        queue<TreeNode*> Que;
        if (!root)
        {
            return res;
        }
        Que.push(root);
        while (!Que.empty())
        {
            vector<int> element;
            int Size = Que.size();
            while (Size--)
            {
                TreeNode* cur = Que.front();
                Que.pop();
                element.push_back(cur->val);
                if (cur->left)
                {
                    Que.push(cur->left);
                }
                if (cur->right)
                {
                    Que.push(cur->right);
                }
            }
            res.push_back(element);    
        }
        return res;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.