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;
}