您的位置:首页 > 编程语言 > C语言/C++

LeetCode:Invert Binary Tree

2015-08-29 22:14 363 查看
Invert a binary tree.
4
/   \
2     7
/ \   / \
1   3 6   9

to
4
/   \
7     2
/ \   / \
9   6 3   1

Trivia:
This problem was inspired by this
original tweet by Max
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.
这道题有一个梗,homebrew的作者去Google面试就被问了这道题,结果他想了半天 没 想 出 来!哈哈哈class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(root==NULL||root->left==NULL&&root->right==NULL){
return root;
}
TreeNode* temp=root->left;
root->left=root->right;
root->right=temp;
if(root->left){
invertTree(root->left);
}
if(root->right){
invertTree(root->right);
}
return root;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ leetcode tree