Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.59 KB | None | 0 0
  1. /**
  2.  * Definition for a binary tree node.
  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. class Solution {
  11. public:
  12.     TreeNode* invertTree(TreeNode* root) {
  13.        
  14.         if(root == NULL) return 0;
  15.        
  16.         if(root->left == NULL && root->right == NULL)
  17.             return root;
  18.        
  19.         root->left = invertTree(root->left);
  20.         root->right = invertTree(root->right);
  21.        
  22.         swap(root->left, root->right);
  23.        
  24.         return root;
  25.     }
  26. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement