Advertisement
i_love_rao_khushboo

Untitled

Aug 31st, 2022
949
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.18 KB | None | 0 0
  1. // Problem: https://www.geeksforgeeks.org/print-path-root-given-node-binary-tree/
  2. // Ref: https://www.youtube.com/watch?v=OJMIa90B7Vc&list=PL7JyMDSI2BfZugpAjdWc8ES_mYMz2F9Qo&index=10
  3. /****************************************************************************************************/
  4.  
  5. #include<bits/stdc++.h>
  6. using namespace std;
  7.  
  8. #define ll long long
  9. #define ull unsigned long long
  10. #define pb push_back
  11. #define mp make_pair
  12. #define F first
  13. #define S second
  14. #define PI 3.1415926535897932384626
  15. #define deb(x) cout << #x << "=" << x << endl
  16. #define deb2(x, y) cout << #x << "=" << x << ", " << #y << "=" << y << endl
  17. typedef pair<int, int> pii;
  18. typedef pair<ll, ll> pll;
  19. typedef vector<int> vi;
  20. typedef vector<ll> vll;
  21. typedef vector<ull> vull;
  22. typedef vector<pii> vpii;
  23. typedef vector<pll> vpll;
  24. typedef vector<vi> vvi;
  25. typedef vector<vll> vvll;
  26. typedef vector<vull> vvull;
  27. mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
  28. int rng(int lim) {
  29.     uniform_int_distribution<int> uid(0,lim-1);
  30.     return uid(rang);
  31. }
  32.  
  33. const int INF = 0x3f3f3f3f;
  34. const int mod = 1e9+7;
  35.  
  36. class TreeNode {
  37.     public:
  38.         int val;
  39.         TreeNode *left;
  40.         TreeNode *right;
  41.         TreeNode(): val(0), left(NULL), right(NULL) {}
  42.         TreeNode(int data): val(data), left(NULL), right(NULL) {}
  43.         TreeNode(int data, TreeNode *left, TreeNode *right): val(data), left(left), right(right) {}
  44. };
  45.  
  46. bool preorder(TreeNode *root, int k, vi &res) {
  47.     if(root == NULL) return 0;
  48.     if(root->val == k) {
  49.         res.pb(root->val);
  50.         return 1;
  51.     }
  52.    
  53.     if(preorder(root->left, k, res)) {
  54.         res.pb(root->val);
  55.         return 1;
  56.     }
  57.    
  58.     if(preorder(root->right, k, res)) {
  59.         res.pb(root->val);
  60.         return 1;
  61.     }
  62.    
  63.     return 0;
  64. }
  65.  
  66. bool is_path(TreeNode *root, int k, vi &res) {
  67.     bool ok = preorder(root, k, res);
  68.     reverse(res.begin(), res.end());
  69.     return ok;
  70. }
  71.  
  72. void solve()
  73. {
  74.     TreeNode* root = new TreeNode(1);
  75.     root->left = new TreeNode(2);
  76.     root->right = new TreeNode(3);
  77.     root->right->left = new TreeNode(4);
  78.     root->right->right = new TreeNode(5);
  79.     root->right->left->left = new TreeNode(6);
  80.     root->right->left->right = new TreeNode(7);
  81.     root->right->left->right->left = new TreeNode(8);
  82.     root->right->left->right->right = new TreeNode(9);
  83.    
  84.     int k; cin >> k;
  85.    
  86.     vi res;
  87.    
  88.     if(is_path(root, k, res)) {
  89.         cout << "YES\n";
  90.         for(auto x: res) cout << x << " ";
  91.         cout << "\n";
  92.     }
  93.    
  94.     else cout << "NO\n";
  95. }
  96.  
  97. int main()
  98. {
  99.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  100.     srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  101.  
  102.     // #ifndef ONLINE_JUDGE
  103.     //     freopen("input.txt", "r", stdin);
  104.     //     freopen("output.txt", "w", stdout);
  105.     // #endif
  106.  
  107.     int t = 1;
  108.     // int test = 1;
  109.     // cin >> t;
  110.     while(t--) {
  111.       // cout << "Case #" << test++ << ": ";
  112.       solve();
  113.     }
  114.  
  115.     return 0;
  116. }
  117.  
  118. // TC: O(n)
  119.  
  120. // NOTE: The preorder function can be simply be made 2 liner as follows --->
  121. /* bool preorder(TreeNode *root, int k, vi &res) {
  122.     if(root == NULL) return 0;
  123.    
  124.     if(root->val == k or preorder(root->left, k, res) or preorder(root->right, k, res)) {
  125.         res.pb(root->val);
  126.         return 1;
  127.     }
  128.    
  129.     return 0;
  130.   }
  131. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement