交换二叉树的左右子树。
直接递归交换左右子树即可,c代码如下:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
struct TreeNode* invertTree(struct TreeNode* root) {
if(root == NULL) return NULL;
struct TreeNode *tmp = root->left;
root->left = root->right;
root->right = tmp;
invertTree(root->left);
invertTree(root->right);
return root;
}
Leetcode 226 Invert Binary Tree
原文地址:http://blog.csdn.net/jeanphorn/article/details/46566909