Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Invert Binary Tree problem solution

YASH PAL, 31 July 202420 January 2026

In this Leetcode Invert Binary Tree problem solution, we have given the root of a binary tree, inverted the tree, and return its root.

Leetcode Invert Binary Tree problem solution

Leetcode Invert Binary Tree problem solution in Python.

class Solution:
    def invertTree(self, root: TreeNode) -> TreeNode:
        if root == None:
            return
        left = root.left
        right = root.right
        root.left, root.right = right, left
        self.invertTree(root.left)
        self.invertTree(root.right)
        
        return root

Invert Binary Tree problem solution in Java.

class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root == null){
            return null;
        }
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        invertTree(root.left);
        invertTree(root.right);
        return root;
        
    }

Problem solution in C++.

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

        TreeNode *l = invertTree(root->left);
        TreeNode *r = invertTree(root->right);
        
        return root;
    }
};

Problem solution in C.

struct TreeNode* invertTree(struct TreeNode* root){
        if (root != NULL) {
            struct TreeNode *tmp;
            tmp = root->right != NULL ? invertTree(root->right) : root->right;
            root->right = root->left != NULL ? invertTree(root->left) : root->left;
            root->left = tmp;
        }
    return root;
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes