Topic:
Invert Binary TreeTotal accepted:20346 Total submissions:57084my submissions QuestionSolution
Invert a binary tree.
4 / 2 7/\ /1 3 6 9
To
4 / 7 2/\ /9 6 3 1
Trivia:
This problem is inspired by this original tweets by Max Howell:
google:90% of our engineers with the software you wrote (Homebrew), but can ' t invert a binary tree on a Whitebo ard so fuck off.
Code:
/** Definition for a binary tree node. * struct TreeNode {* int val; * TreeNode *left; * TreeNode *right; * TreeNode (int x): Val (x), left (null), right (NULL) {} *}; */classSolution { Public: TreeNode* Inverttree (treenode*root) { if(!root)returnRoot; TreeNode* tmp = root->Left ; Root->left = Solution::inverttree (root->Right ); Root->right =Solution::inverttree (TMP); returnRoot; }};
Tips
Flip the two-fork tree and swap two int almost ... The point is to leave a TMP.
"Invert Binary Tree" cpp