nikunjsoni

199

Mar 26th, 2021
64
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. vector<int> ans;
  14. public:
  15.     void dfs(TreeNode *root, int level){
  16.         if(!root) return;
  17.         if(ans.size() < level) ans.push_back(root->val);
  18.         dfs(root->right, level+1);
  19.         dfs(root->left, level+1);
  20.     }
  21.    
  22.     vector<int> rightSideView(TreeNode* root) {
  23.         dfs(root, 1);
  24.         return ans;
  25.     }
  26. };
RAW Paste Data