您的位置:首页 > 其它

Invert Binary Tree

2016-07-22 23:46 295 查看
https://leetcode.com/problems/invert-binary-tree/

Invert a binary tree.

4

/ \

2 7

/ \ / \

1 3 6 9

to

4

/ \

7 2

/ \ / \

9 6 3 1

给出一棵二叉树,求这棵二叉树的镜像。

搬运九章上的实现 http://www.jiuzhang.com/solutions/invert-binary-tree/?source=zhmhw

/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     struct TreeNode *left;
*     struct TreeNode *right;
* };
*/
struct TreeNode* invertTree(struct TreeNode* root) {
if(root == 0)
return  NULL;

struct TreeNode *left = root->left;
struct TreeNode *right = root->right;

root->left = right;
root->right = left;
if(root->left != NULL)
invertTree(root->left);
if(root->right != NULL)
invertTree(root->right);
return root;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: