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