Advertisement
Jathin

Untitled

Sep 29th, 2021
910
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.62 KB | None | 0 0
  1. /**
  2.  * Definition for binary tree
  3.  * struct TreeNode {
  4.  *     int val;
  5.  *     TreeNode *left;
  6.  *     TreeNode *right;
  7.  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8.  * };
  9.  */
  10.  
  11. bool findthepath(TreeNode* A, vector<int> &vec, int B){
  12.     if(A ==  NULL){
  13.         return false;
  14.     }
  15.     if(findthepath(A->left, vec, B) || findthepath(A->right, vec, B) || A->val == B){
  16.         vec.push_back(A->val);
  17.         return true;
  18.     }
  19.     return false;
  20. }
  21. vector<int> Solution::solve(TreeNode* A, int B) {
  22.   vector<int> path;
  23.   findthepath(A, path, B);
  24.   reverse(path.begin(), path.end());
  25.   return path;
  26. }
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement