1.直接把递归把左右子树翻转即可
AC代码:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void invert(TreeNode* root)
{
if (root != NULL)
{
swap(root->left, root->right);
invert(root->left);
invert(root->right);
}
}
TreeNode* invertTree(TreeNode* root) {
invert(root);
return root;
}
};
本文介绍了一种使用递归方法翻转二叉树左右子树的技术,并提供了相应的AC代码实现。
634

被折叠的 条评论
为什么被折叠?



