BFS C++ Solution


  • 0
    vector<vector<int>> verticalOrder(TreeNode* root) {
        list<pair<TreeNode*, int>> lst;
        map<int, vector<int>> track;
        lst.push_back({root,0});
        while( !lst.empty() ) {
            auto p = lst.front();
            lst.pop_front();
            if( p.first ) {
                track[p.second].push_back(p.first->val);
                lst.push_back({p.first->left, p.second-1});
                lst.push_back({p.first->right, p.second+1});
            }
        }
        vector<vector<int>> result;
        for( auto a : track ) result.push_back(a.second);
        return result;
    }

Log in to reply
 

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