knakul853

Untitled

Jul 22nd, 2020
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.43 KB | None | 0 0
  1. /**
  2. knakul853
  3.  */
  4. class Solution {
  5. public:
  6.     vector<int>ans;
  7.     vector<int> rightSideView(TreeNode* root) {
  8.        
  9.         dfs(root, 0);
  10.        
  11.         return ans;
  12.     }
  13.    
  14.     void dfs( TreeNode* root, int lvl)
  15.     {
  16.         if(!root) return;
  17.        
  18.         if((int)ans.size() == lvl) ans.push_back(root->val);
  19.        
  20.         dfs(root->left, lvl+1);
  21.                 dfs(root->right, lvl+1);
  22.  
  23.     }
  24. };
Add Comment
Please, Sign In to add comment