=>
=>
=>
过程如上图,递归交换每个节点的左右子节点(整颗子树都换被交换,本菜鸡先开始以为只单纯交换左右子节点)
代码:
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if (root == NULL)
return root;
swap(root->left, root->right);
invertTree(root->left);
invertTree(root->right);
return root;
}
};