思路很简单,出口是空节点,先翻转子节点,再返回。
TreeNode* invertTree(TreeNode* root) { if (root == nullptr){ return root; } invertTree(root->left); invertTree(root->right); swap(root->left, root->right); return root;}
def invert_tree(root) return root if root == nil invert_tree(root.left) invert_tree(root.right) root.left, root.right = root.right, root.left rootend