题目

226. Invert Binary Tree

QuestionEditorial Solution

My Submissions

Total Accepted:109341

Total Submissions:230799

Difficulty:Easy

Invert a binary tree.

4/\27/\/\1369

to

4/\72/\/\9631

Trivia:
This problem was inspired bythis original tweetbyMax Howell:

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.

/***Definitionforabinarytreenode.*structTreeNode{*intval;*TreeNode*left;*TreeNode*right;*TreeNode(intx):val(x),left(NULL),right(NULL){}*};*/classSolution{public:TreeNode*invertTree(TreeNode*root){TreeNode*p,*q;p=NULL;q=NULL;if(root!=NULL){p=root->left;q=root->right;root->left=q;root->right=p;invertTree(root->left);invertTree(root->right);}returnroot;}};