leetcoed链接:翻转二叉树
思路:
把当前根的左孩子节点与右孩子节点交换,在依次递归向下遍历。
1️⃣ 代码:
void reverseTree(struct TreeNode* root) {
if (root == NULL) {
return;
}
struct TreeNode* temp = root->left;
root->left = root->right;
root->right = temp;
reverseTree(root->left);
reverseTree(root->right);
}
struct TreeNode* invertTree(struct TreeNode* root){
if (root == NULL)
return NULL;
reverseTree(root);
return root;
}