Leetcode 199 Binary Tree Right Side View Solution in c++ | Hindi Coding Community

0

 


Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.



class Solution {
public:

    vector<int> rightSideView(TreeNode* root) {

       vector<int>level;

       if(root == NULL) return level;
       queue<TreeNode*>q;
       q.push(root);

       while(!q.empty())
       {
           
           int n = q.size();
           vector<int>v;
           
           for(int i = 0;i<n;++i)
           {
               auto temp = q.front();
               int val = temp->val;
               q.pop();
               

               if(temp->left!=NULL) q.push(temp->left);
               if(temp->right!=NULL) q.push(temp->right);
           
               v.push_back(val);
           }

            level.push_back(v[v.size()-1]);
       
       }

       return level;
    }
};

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !