Invert a binary tree.
Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void revert(TreeNode* node){
if(NULL != node){
TreeNode *t = node->left;
node->left = node->right;
node->right = t;
revert(node->left);
revert(node->right);
}
}
TreeNode* invertTree(TreeNode* root) {
revert(root);
return root;
}
};