DEV Community

Discussion on: Invert a Binary tree Leetcode solution

Collapse
 
vidit1999 profile image
Vidit Sarkar

Thanks for the problem and solution. Here is my solution,

void invertTree(node* root){
    if(root){
        invertTree(root->left);
        invertTree(root->right);
        swap(root->left, root->right); // #include <algorithm> for this
    }
}

Note: It changes the tree in-place.

Collapse
 
deepa profile image
deepa

gotta say your solution is prettier( and efficient), thanks for sharing!