Advertisement
Guest User

invert_binary_tree.c

a guest
Oct 23rd, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.66 KB | None | 0 0
  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. #define swap_nodes(a, b)    \
  11. ({                          \
  12.     a = (((struct TreeNode *) ((uintptr_t) (a) ^ (uintptr_t) (b)))); \
  13.     b = (((struct TreeNode *) ((uintptr_t) (a) ^ (uintptr_t) (b)))); \
  14.     a = (((struct TreeNode *) ((uintptr_t) (a) ^ (uintptr_t) (b)))); \
  15. })
  16.  
  17. struct TreeNode* invertTree(struct TreeNode* root) {
  18.     if (!root)
  19.         return NULL;
  20.    
  21.     invertTree(root->left);
  22.     invertTree(root->right);
  23.    
  24.     swap_nodes(root->left, root->right);
  25.    
  26.     return root;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement