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 whiteboard so Fuck off.
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 */Ten classSolution { One Public: Atreenode* Inverttree (treenode*root) { -Queue <treenode* >Tree; - if(root==NULL) the returnNULL; - Tree.push (root); - while(!tree.empty ()) - { +treenode* Tmpnode =Tree.front (); - Tree.pop (); +treenode* left=tmpnode->Left ; ATmpnode->left=tmpnode->Right ; attmpnode->right=Left ; - if(tmpnode->Left ) -Tree.push (tmpnode->Left ); - if(tmpnode->Right ) -Tree.push (tmpnode->Right ); - } in returnRoot; - } to};
Invert Binary Tree