💻
June LeetCoding Challenge
  • Introduction
  • Week 1
    • Invert Binary Tree
    • Delete Node in Linked List
    • Two City Scheduling
    • Reverse String
    • Random Pick with Weight
    • Queue Reconstruction by Height
    • Coin Change 2
  • Week 2
    • Power of Two
    • Is Subsequence
    • Search Insert Position
    • Sort Colors
    • Insert Delete GetRandom O(1)
    • Largest Divisible Subset
    • Cheapest Flights Within K Stops
  • Week 3
    • Search in a Binary Search Tree
    • Validate IP Address
    • Surrounded Regions
    • H-Index II
    • Longest Duplicate Substring
    • Permutation Sequence
    • Dungeon Game
  • Week 4
    • Single Number II
    • Count Complete Tree Nodes
    • Unique Binary Search Trees
    • Find the Duplicate Number
    • Sum Root to Leaf Numbers
    • Perfect Squares
    • Reconstruct Itinerary
  • Week 5
    • Unique Paths
    • Word Search II
Powered by GitBook
On this page
  • Problem Statement
  • Testcase
  • Prerequisite Knowledge
  • Binary Tree
  • Level Order Traversal
  • Problem Explanation
  • Inverting a Binary Tree
  • Algorithm
  • Recursive Approach
  • Code

Was this helpful?

  1. Week 1

Invert Binary Tree

LeetCode Problem #226 (Easy)

Problem Statement

Invert a binary tree.

Testcase

Input:
         4
       /   \
      2     7
     / \   / \
    1   3 6   9
    
Output:
         4
       /   \
      7     2
     / \   / \
    9   6 3   1

Prerequisite Knowledge

Binary Tree

A binary tree is a tree data structure in which each parent node has a maximum of two child nodes.

Level Order Traversal

Level Order Tree Traversal is also known as Breadth-First Traversal. In this type of traversal, we traverse through and print the nodes at each level before traversing further down the tree, as shown in the example.

Problem Explanation

Inverting a Binary Tree

An inverted binary tree is a mirror image of the original binary tree. At every level of the binary tree, we must swap the left and right nodes in order to obtain an inverted image of the binary tree.

Algorithm

Recursive Approach

  1. If the tree is empty, return NULL as the inverse of an empty tree is an empty tree.

  2. Recursively traverse through the left and right branches of the tree in a depth-first manner.

  3. When the leaf nodes have been reached, set the respective left and right pointers to NULL.

  4. Backtrack through each level and swap the left and right sub-trees.

  5. The process is completed when the root node is reached.

Time Complexity: O(n)

Code

TreeNode* invertTree(TreeNode* root) {
    // Inverse of an empty tree is an empty tree
    if (root == NULL) {
        return NULL;
    }
        
    // Recursively invert each subtree starting from the root
    TreeNode *right = invertTree(root->right);
    TreeNode *left = invertTree(root->left);
            
    // Exchange the pointers to the left and right nodes
    root->left = right;
    root->right = left;
        
    return root;
}
PreviousIntroductionNextDelete Node in Linked List

Last updated 5 years ago

Was this helpful?

Binary Tree
Level Order Traversal: 1->2->3->4->5
Original Tree vs. Inverted Tree