Advertisement
jayati

Binary Tree Right Side View

May 14th, 2024
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. /**
  2.  * Definition for a binary tree node.
  3.  * struct TreeNode {
  4.  *     int val;
  5.  *     TreeNode *left;
  6.  *     TreeNode *right;
  7.  *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
  8.  *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  9.  *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  10.  * };
  11.  */
  12. class Solution {
  13. public:
  14.      vector<int> v;
  15.     vector<int> rightSideView(TreeNode* root) {
  16.         if(root==NULL)
  17.         {
  18.             return v;
  19.         }
  20.         recursion(root,0);
  21.         return v;
  22.     }
  23.     void recursion(TreeNode* root,int level)
  24.     {
  25.            if(root==NULL)
  26.            {
  27.             return;
  28.            }
  29.            if(level==v.size())
  30.            {
  31.             v.push_back(root->val);
  32.            }
  33.            recursion(root->right,level+1);
  34.            recursion(root->left,level+1);
  35.     }
  36. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement