Leetcode 226. Invert Binary Tree
Invert a binary tree.
题目大意:
置换一棵二叉树,每个结点的左结点和右结点交换。
解题思路:
二叉树递归,设置中间值实现两值交换。时间复杂度O(n+m),其中n为结点数,m为边数。
代码:
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(!root)
return NULL;
TreeNode* temp = root->left;
root->left = root->right;
root->right = temp;
invertTree(root->left);
invertTree(root->right);
return root;
}
};