nikunjsoni

94

Mar 22nd, 2021
74
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. public:
  14.     vector<int> v;
  15.     void dfs(TreeNode* root){
  16.         if(root){
  17.             if(root->left) dfs(root->left);
  18.             v.push_back(root->val);
  19.             if(root->right) dfs(root->right);
  20.         }
  21.     }
  22.     vector<int> inorderTraversal(TreeNode* root) {
  23.         dfs(root);
  24.         return v;
  25.     }
  26. };
RAW Paste Data