翻转二叉树
作者:互联网
一、题目:
二、解题思路:
如果子树不为空将左右节点交换即可。
三、代码和结果:
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(root ==nullptr)return nullptr;
TreeNode *left = invertTree(root->left);
TreeNode *right = invertTree(root->right);
root->left =right;
root->right =left;
return root;
}
};
结果:
标签:right,TreeNode,invertTree,二叉树,return,翻转,root,left 来源: https://blog.csdn.net/sweetorange_/article/details/114819360