Jathin

Untitled

Sep 28th, 2021
614
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. TreeNode* left_extreme(TreeNode* A){
  11.     while(A->right != NULL){
  12.         A = A->right;
  13.     }
  14.     return A;
  15. }
  16. void flat(TreeNode* A){
  17.     if(A->left)
  18.         flat(A->left);
  19.     TreeNode* temp = A->right;
  20.     if(A->left){
  21.         TreeNode* y = left_extreme(A->left);
  22.         y->right  = temp;
  23.         A->right  = A->left;
  24.     }
  25.     A->left = NULL;
  26.     if(temp)
  27.         flat(temp);
  28. }
  29. TreeNode* Solution::flatten(TreeNode* A) {
  30.     TreeNode* head = A;
  31.     flat(A);
  32.     return head;
  33. }
  34.  
RAW Paste Data