Recursive Two Times


  • 0
    F
    #include <vector>
    class Solution {
    public:
    
        int level = 0;
        int first = 0;
        void Transversal(vector<vector<int>> &output, TreeNode* root, int level)
        {
            if (!root) return;
            if (output.size() < (level+1))
            {
                vector<int> suboutput;
                output.push_back(suboutput);
            }
    
            level++;
            if (first)
            {
                output[output.size()-level].push_back(root->val);
            }
            Transversal(output, root->left, level);
            Transversal(output, root->right, level);
    
        }
        vector<vector<int>> levelOrderBottom(TreeNode* root) {
            vector<vector<int>> output;
            Transversal(output, root, level);
            first = 1;
            Transversal(output, root, level);        
            return output;
        }
    };
    

Log in to reply
 

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