G2A Many GEOs
SHARE
TWEET

Untitled

a guest Apr 8th, 2020 123 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.  *     struct TreeNode *left;
  6.  *     struct TreeNode *right;
  7.  * };
  8.  */
  9.  
  10.  
  11. /**
  12.  * Note: The returned array must be malloced, assume caller calls free().
  13.  */
  14.  
  15. int n, *arr;
  16.  
  17. void getElements(struct TreeNode *root){
  18.     if(!root)
  19.         return;
  20.     getElements(root->left);
  21.     getElements(root->right);
  22.     arr[n++] = root->val;
  23. }
  24.  
  25. int compare (const void * a, const void * b)
  26. {
  27.   return ( *(int*)a - *(int*)b );
  28. }
  29.  
  30. int* getAllElements(struct TreeNode* root1, struct TreeNode* root2, int* returnSize){
  31.     arr = malloc(10000 * sizeof(int));
  32.     n = 0;
  33.     getElements(root1);
  34.     getElements(root2);
  35.     qsort(arr, n, sizeof(int), compare);
  36.     *returnSize = n;
  37.     return arr;
  38. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top