

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- How to check whether a binary tree is a valid binary search tree using recursion in C#?
- Invert Binary Tree in Python
- Python Program for Depth First Binary Tree Search using Recursion
- 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++?
- Optimal Binary Search Tree
- Binary Search Tree in Javascript
- Balance a Binary Search Tree in c++
- Implementing a Binary Search Tree in JavaScript
- Binary Search Tree to Greater Sum Tree in C++
- Python Program to Implement Binary Search with Recursion
- Python Program to Implement Binary Search without Recursion
Advertisements