4ms C++ solution easy to understand


  • 0
    N

    Pre-order traversal. Use a global varible rows to track the total number of the rows the return vector has.

    class Solution {
        public:
            vector<vector<int>> levelOrder(TreeNode* root) {
                vector<vector <int>> res;
                if(root==NULL) return res;
                helper(root, res, 0);
                return res;
            }
            
        private:
            int rows = 0;
            void helper(TreeNode* node, vector<vector<int>> &res, int count){
                if(node == NULL) return;
                if(count >= rows){
                    vector<int> newRow;
                    newRow.push_back(node->val);
                    res.push_back(newRow);
                    rows++;
                }
                else{
                    res[count].push_back(node->val);
                }
                helper(node->left, res, count + 1);
                helper(node->right, res, count + 1);
                return; 
            }
        };

  • 0
    L
    This post is deleted!

Log in to reply
 

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