
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to invert a binary search tree using recursion in C#?
To invert a binary search tree, we call a method InvertABinarySearchTree which takes node as a parameter. If the node is null then return null, if the node is not null, we call the InvertABinarySearchTree recursively by passing the left and right child values. and assign the right child value to the left child and left child value to the right child. The final output will consist of the tree which will be its own mirror image.
Example
public class TreesPgm{ public class Node{ public int Value; public Node LeftChild; public Node RightChild; public Node(int value){ this.Value = value; } public override String ToString(){ return "Node=" + Value; } } public Node InvertABinarySearchTree(Node node){ if (node == null){ return null; } Node left = InvertABinarySearchTree(node.LeftChild); Node right = InvertABinarySearchTree(node.RightChild); node.LeftChild = right; node.RightChild = left; return root; } }
Input
1 3 2
Output
1 2 3
- Related Articles
- How to check whether a binary tree is a valid binary search tree using recursion in C#?
- Python Program for Depth First Binary Tree Search using Recursion
- Invert Binary Tree in Python
- Program to invert a binary tree in Python
- Binary Tree to Binary Search Tree Conversion using STL set C++?
- Binary Tree to Binary Search Tree Conversion in C++
- Python Program to Sort using a Binary Search Tree
- How to invert a binary image in OpenCV using C++?
- Difference between Binary Tree and Binary Search Tree
- C++ Program to Implement a Binary Search Tree using Linked Lists
- Binary Search Tree to Greater Sum Tree in C++
- Binary Search Tree in Javascript
- Optimal Binary Search Tree
- Balance a Binary Search Tree in c++
- Implementing a Binary Search Tree in JavaScript

Advertisements