Invert Binary Tree into its mirror tree.(cpp,leetcode)

Invert Binary Tree into its mirror tree.(cpp,leetcode)

 

input:

     4                                        
   /   \
  2     7       
 / \   / \
1   3 6   9
 

output:

     4
   /   \
  7     2
 / \   / \
9   6 3   1

===============================================

code:-

 
class Solution {
public:
    void xx(TreeNode *root)
    {
         if(root==NULL)
            return ;
        else
        {
            invertTree(root->left);
            invertTree(root->right);
            swap(root->left,root->right);
           
        }
    }
    TreeNode* invertTree(TreeNode* root)
    {
        xx(root);
        return root;
      
       
       
    }
};

 

 

 

 

Comments

Popular posts from this blog

Amazing Subarrays(cpp,interviewbit)

Symmetric Tree(leetcode,cpp):

sum of left leaves in a tree(leetcode).