您的位置:首页 > 其它

leetcode[226]:Invert Binary Tree

2015-06-12 20:22 387 查看
Invert Binary Tree



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.

struct TreeNode* invertTree(struct TreeNode* root) {

struct TreeNode *tmp1;
if(!root) return root;
if(!root->left && !root->right) return root;
if( root->left && !root->right)
{
root->right=invertTree(root->left);;
root->left = NULL;
}
else if( !root->left && root->right)
{
root->left=invertTree(root->right);
root->right = NULL;
}
else if( root->left && root->right)
{
tmp1 = root->left;
root->left = invertTree(root->right);
root->right = invertTree(tmp1);
}
return root;
}


递归交换左右子树,注意空的情况。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  binary tree