LeetCode | 226. 翻转二叉树
OJ链接
- 不为空就翻转,空空就停止翻转
- 左子树的节点给了右子树
- 右子树的节点给了左就完成了翻转
struct TreeNode* invertTree(struct TreeNode* root) {
//不为空就进行翻转
if(root)
{
//翻转
struct TreeNode* tmp = root->left;
root->left = root->right;
root->right = tmp;
//遍历左子树和右子树
invertTree(root->left);
invertTree(root->right);
}
//返回根节点
return root;
}