LeetCode 226. 翻转二叉树
程序员文章站
2022-03-03 10:12:54
...
题目描述
翻转一棵二叉树
示例:
输入:
4
/ \
2 7
/ \ / \
1 3 6 9
输出:
4
/ \
7 2
/ \ / \
9 6 3 1
思路
翻转二叉树的左右子树,然后递归
class Solution {
public:
TreeNode* invertTree(TreeNode* root)
{
if(root == NULL)
return root;
TreeNode *temp = root->left;
root->left = root->right;
root->right = temp
invertTree(root->left);
invertTree(root->right);
return root;
}
};
推荐阅读