给你一棵二叉树的根节点 root
,翻转这棵二叉树,并返回其根节点。
/*** Definition for a binary tree node.* struct TreeNode {* int val;* struct TreeNode *left;* struct TreeNode *right;* };*/
struct TreeNode* invertTree(struct TreeNode* root) {if(!root){return NULL;}//struct TreeNode* tmp=(struct TreeNode*)malloc(sizeof(struct TreeNode*));//一个tmp指针不需要malloc,可直接定义struct TreeNode* tmp=root->left;root->left=root->right;root->right=tmp;invertTree(root->left);invertTree(root->right);return root;}