Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth 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 *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

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