Inverting Binary Tree Technique

AlgorithmAlgorithmBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

Inverting a binary tree means swapping all left and right node pairs. This is a common problem in computer science and is often used as an interview question.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL algorithm(("`Algorithm`")) -.-> algorithm/BasicAlgorithmsGroup(["`Basic Algorithms`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) python(("`Python`")) -.-> python/ErrorandExceptionHandlingGroup(["`Error and Exception Handling`"]) algorithm/BasicAlgorithmsGroup -.-> algorithm/graphs_trees("`Graphs Trees`") algorithm/BasicAlgorithmsGroup -.-> algorithm/recursion_dynamic("`Recursion Dynamic`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/strings("`Strings`") python/BasicConceptsGroup -.-> python/type_conversion("`Type Conversion`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/default_arguments("`Default Arguments`") python/ObjectOrientedProgrammingGroup -.-> python/classes_objects("`Classes and Objects`") python/ObjectOrientedProgrammingGroup -.-> python/constructor("`Constructor`") python/ObjectOrientedProgrammingGroup -.-> python/polymorphism("`Polymorphism`") python/ObjectOrientedProgrammingGroup -.-> python/encapsulation("`Encapsulation`") python/ErrorandExceptionHandlingGroup -.-> python/raising_exceptions("`Raising Exceptions`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills algorithm/graphs_trees -.-> lab-268832{{"`Inverting Binary Tree Technique`"}} algorithm/recursion_dynamic -.-> lab-268832{{"`Inverting Binary Tree Technique`"}} python/variables_data_types -.-> lab-268832{{"`Inverting Binary Tree Technique`"}} python/strings -.-> lab-268832{{"`Inverting Binary Tree Technique`"}} python/type_conversion -.-> lab-268832{{"`Inverting Binary Tree Technique`"}} python/conditional_statements -.-> lab-268832{{"`Inverting Binary Tree Technique`"}} python/tuples -.-> lab-268832{{"`Inverting Binary Tree Technique`"}} python/function_definition -.-> lab-268832{{"`Inverting Binary Tree Technique`"}} python/default_arguments -.-> lab-268832{{"`Inverting Binary Tree Technique`"}} python/classes_objects -.-> lab-268832{{"`Inverting Binary Tree Technique`"}} python/constructor -.-> lab-268832{{"`Inverting Binary Tree Technique`"}} python/polymorphism -.-> lab-268832{{"`Inverting Binary Tree Technique`"}} python/encapsulation -.-> lab-268832{{"`Inverting Binary Tree Technique`"}} python/raising_exceptions -.-> lab-268832{{"`Inverting Binary Tree Technique`"}} python/build_in_functions -.-> lab-268832{{"`Inverting Binary Tree Technique`"}} end

Invert Tree

Problem

Given a binary tree, write a function to invert the tree. The function should take the root node of the tree as input and return the new root node of the inverted tree.

Requirements

To solve this problem, you need to meet the following requirements:

  • You should have a Node class that represents a node in the binary tree.
  • You should swap all left and right node pairs in the binary tree.
  • You should handle invalid inputs gracefully.
  • Your solution should fit in memory.

Example Usage

Suppose we have the following binary tree:

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

After inverting the tree, we should get:

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

Summary

Inverting a binary tree is a common problem in computer science. By swapping all left and right node pairs, we can create a new tree that is the mirror image of the original tree. To solve this problem, we need to have a Node class that represents a node in the binary tree, and we need to handle invalid inputs gracefully.

Other Algorithm Tutorials you may like