4ms c++ solution


  • 0
    P
    class Solution {
        int depth(TreeNode *rt){
            if(!rt) return 0;
            return max(depth(rt->left),depth(rt->right))+1;
        }
        void mapping(TreeNode *rt,vector<vector<int> >&rst,int lev){
            if(rt){
                rst[lev].push_back(rt->val);
                mapping(rt->left,rst,lev+1);
                mapping(rt->right,rst,lev+1);
            }
        }
    public:
        vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
            vector<vector<int> > rst(depth(root));
            mapping(root,rst,0);
            for(int i=1;i<rst.size();i+=2) reverse(rst[i].begin(),rst[i].end());
            return rst;
        }
    };

Log in to reply
 

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