c++ 6ms solution


  • 0
    A
        /**
    
    • Definition for a binary tree node.
    • struct TreeNode {
    • int val;
      
    • TreeNode *left;
      
    • TreeNode *right;
      
    • TreeNode(int x) : val(x), left(NULL), right(NULL) {}
      
    • };
      */

    int height(TreeNode *root)
    {
    if(root==NULL)
    return 0;
    return max(height(root->left),height(root->right))+1;
    }

    void column(TreeNode *root,int h,vector<int>& v1)
    {
    if(root==NULL)
    return;
    if(h==1)
    {
    v1.push_back(root->val);
    return;
    }
    column(root->left,h-1,v1);
    column(root->right,h-1,v1);
    }

    class Solution {
    public:
    vector<vector<int>> levelOrderBottom(TreeNode* root) {
    vector<vector <int> > v;

    vector<int> v1;
    for(int h=height(root);h>=1;h--)
    {
        column(root,h,v1);
        v.push_back(v1);
        v1.clear();
    }
    return v;
    }
    

    };


Log in to reply
 

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