226. Invert Binary Tree
Invert a binary tree.
Example:
Input:
4
/ \
2 7
/ \ / \
1 3 6 9Output:
4
/ \
7 2
/ \ / \
9 6 3 1// Recursion
TreeNode* invertTree(TreeNode* root) { // time: O(n); space: O(n)
if (!root) return root;
TreeNode* left = invertTree(root->left);
root->left = invertTree(root->right);
root->right = left;
return root;
}Last updated
Was this helpful?