Simple C++ code


  • -4
    E

    class Solution {
    public:
    vector<vector<int> > levelOrder(TreeNode* root) {
    vector<vector<int> > ans;
    if(!root)
    return ans;
    vector<TreeNode > ss(1,root);
    while(!ss.empty()){
    vector<int> one;
    vector<TreeNode
    > ne;
    for(int i=0;i<ss.size();i++){
    one.push_back(ss[i]->val);
    if(ss[i]->left)
    ne.push_back(ss[i]->left);
    if(ss[i]->right)
    ne.push_back(ss[i]->right);
    }
    ans.push_back(one);
    ss=ne;
    }
    return ans;
    }
    };


Log in to reply
 

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