Reverse inorder traversal using morris traversal

Here given code implementation process.

// C program for 
// Reverse inorder traversal using morris traversal
#include <stdio.h>
#include <stdlib.h>

// Tree Node
struct TreeNode
{
	int data;
	struct TreeNode *left;
	struct TreeNode *right;
};
// Binary Tree
struct BinaryTree
{
	struct TreeNode *root;
};
// Create new tree
struct BinaryTree *new_tree()
{
	// Create dynamic node
	struct BinaryTree *tree = (struct BinaryTree *) malloc(sizeof(struct BinaryTree));
	if (tree != NULL)
	{
		tree->root = NULL;
	}
	else
	{
		printf("Memory Overflow to Create tree Tree\n");
	}
	//return new tree
	return tree;
}
// This is creates and returns the new binary tree node
struct TreeNode *get_node(int data)
{
	// Create dynamic node
	struct TreeNode *node = (struct TreeNode *) malloc(sizeof(struct TreeNode));
	if (node != NULL)
	{
		//Set data and pointer values
		node->data = data;
		node->left = NULL;
		node->right = NULL;
	}
	else
	{
		//This is indicates, segmentation fault or memory overflow problem
		printf("Memory Overflow\n");
	}
	//return new node
	return node;
}
// This is display the reverse inorder traversal
void reverseInorder(struct TreeNode *root)
{
	if (root == NULL)
	{
		return;
	}
	// Start to root node of tree
	struct TreeNode *current = root;
	struct TreeNode *auxiliary = NULL;
	printf("\n  Morris Reverse Inorder \n");
	// iterating tree nodes
	while (current != NULL)
	{
		if (current->right == NULL)
		{
			// print node value
			printf("  %d", current->data);
			// When right child are empty then visit to left child
			current = current->left;
		}
		else
		{
			auxiliary = current->right;
			while (auxiliary->left != NULL && auxiliary->left != current)
			{
				auxiliary = auxiliary->left;
			}
			if (auxiliary->left != current)
			{
				// Change link
				auxiliary->left = current;
				// Get current node is right child
				current = current->right;
			}
			else
			{
				// print node value
				printf("  %d", current->data);
				// unlink
				auxiliary->left = NULL;
				// visit to left child
				current = current->left;
			}
		}
	}
}
int main()
{
	struct BinaryTree *tree = new_tree();
	/*
	Create Binary Tree

	         4
	       /   \
	      8     3
	     / \   / \
	   10   6 7   2
	       /   \   \
	      9     5  -9
	*/
	// Add tree node
	tree->root = get_node(4);
	tree->root->left = get_node(8);
	tree->root->left->left = get_node(10);
	tree->root->right = get_node(3);
	tree->root->right->right = get_node(2);
	tree->root->right->right->right = get_node(-9);
	tree->root->right->left = get_node(7);
	tree->root->right->left->right = get_node(5);
	tree->root->left->right = get_node(6);
	tree->root->left->right->left = get_node(9);
	// Test
	reverseInorder(tree->root);
	return 0;
}

input

  Morris Reverse Inorder
  -9  2  3  5  7  4  6  9  8  10
/*
    Java Program
    Reverse inorder traversal using morris traversal
*/
// Binary Tree node
class TreeNode
{
	public int data;
	public TreeNode left;
	public TreeNode right;
	public TreeNode(int data)
	{
		// Set node value
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
class BinaryTree
{
	public TreeNode root;
	public BinaryTree()
	{
		// Set initial tree root to null
		this.root = null;
	}
	// This is display the reverse inorder traversal
	public void reverseInorder()
	{
		if (this.root == null)
		{
			return;
		}
		// Start to root node of tree
		TreeNode current = this.root;
		TreeNode auxiliary = null;
		System.out.print("\n Morris Reverse Inorder \n");
		// iterating tree nodes
		while (current != null)
		{
			if (current.right == null)
			{
				// print node value
				System.out.print("  " + current.data);
				// When right child are empty then visit to left child
				current = current.left;
			}
			else
			{
				auxiliary = current.right;
				while (auxiliary.left != null && auxiliary.left != current)
				{
					auxiliary = auxiliary.left;
				}
				if (auxiliary.left != current)
				{
					// Change link
					auxiliary.left = current;
					// Get current node is right child
					current = current.right;
				}
				else
				{
					// print node value
					System.out.print("  " + current.data);
					// unlink
					auxiliary.left = null;
					// visit to left child
					current = current.left;
				}
			}
		}
	}
	public static void main(String[] args)
	{
		// Create new binary tree 
		BinaryTree tree = new BinaryTree();
		/*
		    Construct tree

		         4
		       /   \
		      8     3
		     / \   / \
		   10   6 7   2
		       /   \   \
		      9     5  -9
		*/
		// Add tree node
		tree.root = new TreeNode(4);
		tree.root.left = new TreeNode(8);
		tree.root.left.left = new TreeNode(10);
		tree.root.right = new TreeNode(3);
		tree.root.right.right = new TreeNode(2);
		tree.root.right.right.right = new TreeNode(-9);
		tree.root.right.left = new TreeNode(7);
		tree.root.right.left.right = new TreeNode(5);
		tree.root.left.right = new TreeNode(6);
		tree.root.left.right.left = new TreeNode(9);
		// Test
		tree.reverseInorder();
	}
}

input

 Morris Reverse Inorder
  -9  2  3  5  7  4  6  9  8  10
// Include header file
#include <iostream>
using namespace std;
/*
    C++ Program
    Reverse inorder traversal using morris traversal
*/

// Binary Tree node
class TreeNode
{
	public: 
    int data;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int data)
	{
		// Set node value
		this->data = data;
		this->left = NULL;
		this->right = NULL;
	}
};
class BinaryTree
{
	public: 
    TreeNode *root;
	BinaryTree()
	{
		this->root = NULL;
	}
	// This is display the reverse inorder traversal
	void reverseInorder()
	{
		if (this->root == NULL)
		{
			return;
		}
		// Start to root node of tree
		TreeNode *current = this->root;
		TreeNode *auxiliary = NULL;
		cout << "\n Morris Reverse Inorder \n";
		// iterating tree nodes
		while (current != NULL)
		{
			if (current->right == NULL)
			{
				// print node value
				cout << "  " << current->data;
				// When right child are empty then visit to left child
				current = current->left;
			}
			else
			{
				auxiliary = current->right;
				while (auxiliary->left != NULL && auxiliary->left != current)
				{
					auxiliary = auxiliary->left;
				}
				if (auxiliary->left != current)
				{
					// Change link
					auxiliary->left = current;
					// Get current node is right child
					current = current->right;
				}
				else
				{
					// print node value
					cout << "  " << current->data;
					// unlink
					auxiliary->left = NULL;
					// visit to left child
					current = current->left;
				}
			}
		}
	}
};
int main()
{
	// Create new binary tree
	BinaryTree *tree = new BinaryTree();
	/*
	    Construct tree
	         4
	       /   \
	      8     3
	     / \   / \
	   10   6 7   2
	       /   \   \
	      9     5  -9
	*/
	// Add tree node
	tree->root = new TreeNode(4);
	tree->root->left = new TreeNode(8);
	tree->root->left->left = new TreeNode(10);
	tree->root->right = new TreeNode(3);
	tree->root->right->right = new TreeNode(2);
	tree->root->right->right->right = new TreeNode(-9);
	tree->root->right->left = new TreeNode(7);
	tree->root->right->left->right = new TreeNode(5);
	tree->root->left->right = new TreeNode(6);
	tree->root->left->right->left = new TreeNode(9);
	// Test
	tree->reverseInorder();
	return 0;
}

input

 Morris Reverse Inorder
  -9  2  3  5  7  4  6  9  8  10
// Include namespace system
using System;

/*
    Csharp Program
    Reverse inorder traversal using morris traversal
*/

// Binary Tree node
public class TreeNode
{
	public int data;
	public TreeNode left;
	public TreeNode right;
	public TreeNode(int data)
	{
		// Set node value
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
public class BinaryTree
{
	public TreeNode root;
	public BinaryTree()
	{
		// Set initial tree root to null
		this.root = null;
	}
	// This is display the reverse inorder traversal
	public void reverseInorder()
	{
		if (this.root == null)
		{
			return;
		}
		// Start to root node of tree
		TreeNode current = this.root;
		TreeNode auxiliary = null;
		Console.Write("\n Morris Reverse Inorder \n");
		// iterating tree nodes
		while (current != null)
		{
			if (current.right == null)
			{
				// print node value
				Console.Write("  " + current.data);
				// When right child are empty then visit to left child
				current = current.left;
			}
			else
			{
				auxiliary = current.right;
				while (auxiliary.left != null && auxiliary.left != current)
				{
					auxiliary = auxiliary.left;
				}
				if (auxiliary.left != current)
				{
					// Change link
					auxiliary.left = current;
					// Get current node is right child
					current = current.right;
				}
				else
				{
					// print node value
					Console.Write("  " + current.data);
					// unlink
					auxiliary.left = null;
					// visit to left child
					current = current.left;
				}
			}
		}
	}
	public static void Main(String[] args)
	{
		// Create new binary tree
		BinaryTree tree = new BinaryTree();
		/*
		    Construct tree
		         4
		       /   \
		      8     3
		     / \   / \
		   10   6 7   2
		       /   \   \
		      9     5  -9
		*/
		// Add tree node
		tree.root = new TreeNode(4);
		tree.root.left = new TreeNode(8);
		tree.root.left.left = new TreeNode(10);
		tree.root.right = new TreeNode(3);
		tree.root.right.right = new TreeNode(2);
		tree.root.right.right.right = new TreeNode(-9);
		tree.root.right.left = new TreeNode(7);
		tree.root.right.left.right = new TreeNode(5);
		tree.root.left.right = new TreeNode(6);
		tree.root.left.right.left = new TreeNode(9);
		// Test
		tree.reverseInorder();
	}
}

input

 Morris Reverse Inorder
  -9  2  3  5  7  4  6  9  8  10
<?php
/*
    Php Program
    Reverse inorder traversal using morris traversal
*/

// Binary Tree node
class TreeNode
{
	public $data;
	public $left;
	public $right;
	public	function __construct($data)
	{
		// Set node value
		$this->data = $data;
		$this->left = NULL;
		$this->right = NULL;
	}
}
class BinaryTree
{
	public $root;
	public	function __construct()
	{
		$this->root = NULL;
	}
	// This is display the reverse inorder traversal
	public	function reverseInorder()
	{
		if ($this->root == NULL)
		{
			return;
		}
		// Start to root node of tree
		$current = $this->root;
		$auxiliary = NULL;
		echo("\n Morris Reverse Inorder \n");
		// iterating tree nodes
		while ($current != NULL)
		{
			if ($current->right == NULL)
			{
				// print node value
				echo("  ".$current->data);
				// When right child are empty then visit to left child
				$current = $current->left;
			}
			else
			{
				$auxiliary = $current->right;
				while ($auxiliary->left != NULL && $auxiliary->left != $current)
				{
					$auxiliary = $auxiliary->left;
				}
				if ($auxiliary->left != $current)
				{
					// Change link
					$auxiliary->left = $current;
					// Get current node is right child
					$current = $current->right;
				}
				else
				{
					// print node value
					echo("  ".$current->data);
					// unlink
					$auxiliary->left = NULL;
					// visit to left child
					$current = $current->left;
				}
			}
		}
	}
}

function main()
{
	// Create new binary tree
	$tree = new BinaryTree();
	/*
	    Construct tree
	         4
	       /   \
	      8     3
	     / \   / \
	   10   6 7   2
	       /   \   \
	      9     5  -9
	*/
	// Add tree node
	$tree->root = new TreeNode(4);
	$tree->root->left = new TreeNode(8);
	$tree->root->left->left = new TreeNode(10);
	$tree->root->right = new TreeNode(3);
	$tree->root->right->right = new TreeNode(2);
	$tree->root->right->right->right = new TreeNode(-9);
	$tree->root->right->left = new TreeNode(7);
	$tree->root->right->left->right = new TreeNode(5);
	$tree->root->left->right = new TreeNode(6);
	$tree->root->left->right->left = new TreeNode(9);
	// Test
	$tree->reverseInorder();
}
main();

input

 Morris Reverse Inorder
  -9  2  3  5  7  4  6  9  8  10
/*
    Node JS Program
    Reverse inorder traversal using morris traversal
*/
// Binary Tree node
class TreeNode
{
	constructor(data)
	{
		// Set node value
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
class BinaryTree
{
	constructor()
	{
		this.root = null;
	}
	// This is display the reverse inorder traversal
	reverseInorder()
	{
		if (this.root == null)
		{
			return;
		}
		// Start to root node of tree
		var current = this.root;
		var auxiliary = null;
		process.stdout.write("\n Morris Reverse Inorder \n");
		// iterating tree nodes
		while (current != null)
		{
			if (current.right == null)
			{
				// print node value
				process.stdout.write("  " + current.data);
				// When right child are empty then visit to left child
				current = current.left;
			}
			else
			{
				auxiliary = current.right;
				while (auxiliary.left != null && auxiliary.left != current)
				{
					auxiliary = auxiliary.left;
				}
				if (auxiliary.left != current)
				{
					// Change link
					auxiliary.left = current;
					// Get current node is right child
					current = current.right;
				}
				else
				{
					// print node value
					process.stdout.write("  " + current.data);
					// unlink
					auxiliary.left = null;
					// visit to left child
					current = current.left;
				}
			}
		}
	}
}

function main()
{
	// Create new binary tree
	var tree = new BinaryTree();
	/*
	    Construct tree
	         4
	       /   \
	      8     3
	     / \   / \
	   10   6 7   2
	       /   \   \
	      9     5  -9
	*/
	// Add tree node
	tree.root = new TreeNode(4);
	tree.root.left = new TreeNode(8);
	tree.root.left.left = new TreeNode(10);
	tree.root.right = new TreeNode(3);
	tree.root.right.right = new TreeNode(2);
	tree.root.right.right.right = new TreeNode(-9);
	tree.root.right.left = new TreeNode(7);
	tree.root.right.left.right = new TreeNode(5);
	tree.root.left.right = new TreeNode(6);
	tree.root.left.right.left = new TreeNode(9);
	// Test
	tree.reverseInorder();
}
main();

input

 Morris Reverse Inorder
  -9  2  3  5  7  4  6  9  8  10
#    Python 3 Program
#    Reverse inorder traversal using morris traversal

#  Binary Tree node
class TreeNode :
	def __init__(self, data) :
		#  Set node value
		self.data = data
		self.left = None
		self.right = None
	

class BinaryTree :
	def __init__(self) :
		self.root = None
	
	#  This is display the reverse inorder traversal
	def reverseInorder(self) :
		if (self.root == None) :
			return
		
		current = self.root
		auxiliary = None
		print("\n Morris Reverse Inorder ")
		#  iterating tree nodes
		while (current != None) :
			if (current.right == None) :
				#  print node value
				print(" ", current.data, end = "")
				#  When right child are empty then visit to left child
				current = current.left
			else :
				auxiliary = current.right
				while (auxiliary.left != None and auxiliary.left != current) :
					auxiliary = auxiliary.left
				
				if (auxiliary.left != current) :
					#  Change link
					auxiliary.left = current
					#  Get current node is right child
					current = current.right
				else :
					#  print node value
					print(" ", current.data, end = "")
					#  unlink
					auxiliary.left = None
					#  visit to left child
					current = current.left
				
			
		
	

def main() :
	tree = BinaryTree()
	#    Construct tree
	#         4
	#       /   \
	#      8     3
	#     / \   / \
	#   10   6 7   2
	#       /   \   \
	#      9     5  -9
	#  Add tree node
	tree.root = TreeNode(4)
	tree.root.left = TreeNode(8)
	tree.root.left.left = TreeNode(10)
	tree.root.right = TreeNode(3)
	tree.root.right.right = TreeNode(2)
	tree.root.right.right.right = TreeNode(-9)
	tree.root.right.left = TreeNode(7)
	tree.root.right.left.right = TreeNode(5)
	tree.root.left.right = TreeNode(6)
	tree.root.left.right.left = TreeNode(9)
	#  Test
	tree.reverseInorder()

if __name__ == "__main__": main()

input

 Morris Reverse Inorder
  -9  2  3  5  7  4  6  9  8  10
#    Ruby Program
#    Reverse inorder traversal using morris traversal

#  Binary Tree node
class TreeNode 
	# Define the accessor and reader of class TreeNode
	attr_reader :data, :left, :right
	attr_accessor :data, :left, :right
	def initialize(data) 
		#  Set node value
		self.data = data
		self.left = nil
		self.right = nil
	end

end

class BinaryTree 
	# Define the accessor and reader of class BinaryTree
	attr_reader :root
	attr_accessor :root
	def initialize() 
		self.root = nil
	end

	#  This is display the reverse inorder traversal
	def reverseInorder() 
		if (self.root == nil) 
			return
		end

		#  Start to root node of tree
		current = self.root
		auxiliary = nil
		print("\n Morris Reverse Inorder \n")
		#  iterating tree nodes
		while (current != nil) 
			if (current.right == nil) 
				#  print node value
				print("  ", current.data)
				#  When right child are empty then visit to left child
				current = current.left
			else 
				auxiliary = current.right
				while (auxiliary.left != nil && auxiliary.left != current) 
					auxiliary = auxiliary.left
				end

				if (auxiliary.left != current) 
					#  Change link
					auxiliary.left = current
					#  Get current node is right child
					current = current.right
				else 
					#  print node value
					print("  ", current.data)
					#  unlink
					auxiliary.left = nil
					#  visit to left child
					current = current.left
				end

			end

		end

	end

end

def main() 
	#  Create new binary tree
	tree = BinaryTree.new()
	#    Construct tree
	#         4
	#       /   \
	#      8     3
	#     / \   / \
	#   10   6 7   2
	#       /   \   \
	#      9     5  -9
	#  Add tree node
	tree.root = TreeNode.new(4)
	tree.root.left = TreeNode.new(8)
	tree.root.left.left = TreeNode.new(10)
	tree.root.right = TreeNode.new(3)
	tree.root.right.right = TreeNode.new(2)
	tree.root.right.right.right = TreeNode.new(-9)
	tree.root.right.left = TreeNode.new(7)
	tree.root.right.left.right = TreeNode.new(5)
	tree.root.left.right = TreeNode.new(6)
	tree.root.left.right.left = TreeNode.new(9)
	#  Test
	tree.reverseInorder()
end

main()

input

 Morris Reverse Inorder 
  -9  2  3  5  7  4  6  9  8  10
/*
    Scala Program
    Reverse inorder traversal using morris traversal
*/

// Binary Tree node
class TreeNode(var data: Int , var left: TreeNode , var right: TreeNode)
{
	def this(data: Int)
	{
		// Set node value
		this(data,null,null);
	}
}
class BinaryTree(var root: TreeNode)
{
	def this()
	{
		this(null);
	}
	// This is display the reverse inorder traversal
	def reverseInorder(): Unit = {
		if (this.root == null)
		{
			return;
		}
		// Start to root node of tree
		var current: TreeNode = this.root;
		var auxiliary: TreeNode = null;
		print("\n Morris Reverse Inorder \n");
		// iterating tree nodes
		while (current != null)
		{
			if (current.right == null)
			{
				// print node value
				print("  " + current.data);
				// When right child are empty then visit to left child
				current = current.left;
			}
			else
			{
				auxiliary = current.right;
				while (auxiliary.left != null && auxiliary.left != current)
				{
					auxiliary = auxiliary.left;
				}
				if (auxiliary.left != current)
				{
					// Change link
					auxiliary.left = current;
					// Get current node is right child
					current = current.right;
				}
				else
				{
					// print node value
					print("  " + current.data);
					// unlink
					auxiliary.left = null;
					// visit to left child
					current = current.left;
				}
			}
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		// Create new binary tree
		var tree: BinaryTree = new BinaryTree();
		/*
		    Construct tree
		         4
		       /   \
		      8     3
		     / \   / \
		   10   6 7   2
		       /   \   \
		      9     5  -9
		*/
		// Add tree node
		tree.root = new TreeNode(4);
		tree.root.left = new TreeNode(8);
		tree.root.left.left = new TreeNode(10);
		tree.root.right = new TreeNode(3);
		tree.root.right.right = new TreeNode(2);
		tree.root.right.right.right = new TreeNode(-9);
		tree.root.right.left = new TreeNode(7);
		tree.root.right.left.right = new TreeNode(5);
		tree.root.left.right = new TreeNode(6);
		tree.root.left.right.left = new TreeNode(9);
		// Test
		tree.reverseInorder();
	}
}

input

 Morris Reverse Inorder
  -9  2  3  5  7  4  6  9  8  10
/*
    Swift 4 Program
    Reverse inorder traversal using morris traversal
*/
// Binary Tree node
class TreeNode
{
	var data: Int;
	var left: TreeNode? ;
	var right: TreeNode? ;
	init(_ data: Int)
	{
		// Set node value
		self.data = data;
		self.left = nil;
		self.right = nil;
	}
}
class BinaryTree
{
	var root: TreeNode? ;
	init()
	{
		self.root = nil;
	}
	// This is display the reverse inorder traversal
	func reverseInorder()
	{
		if (self.root == nil)
		{
			return;
		}
		// Start to root node of tree
		var current: TreeNode? = self.root;
		var auxiliary: TreeNode? = nil;
		print("\n Morris Reverse Inorder ");
		// iterating tree nodes
		while (current  != nil)
		{
			if (current!.right == nil)
			{
				// print node value
				print("  ", current!.data, terminator: "");
				// When right child are empty then visit to left child
				current = current!.left;
			}
			else
			{
				auxiliary = current!.right;
				while (auxiliary!.left  != nil 
                       && !(auxiliary!.left  === current))
				{
					auxiliary = auxiliary!.left;
				}
				if (!(auxiliary!.left === current))
				{
					// Change link
					auxiliary!.left = current;
					// Get current node is right child
					current = current!.right;
				}
				else
				{
					// print node value
					print("  ", current!.data, terminator: "");
					// unlink
					auxiliary!.left = nil;
					// visit to left child
					current = current!.left;
				}
			}
		}
	}
}
func main()
{
	// Create new binary tree
	let tree: BinaryTree = BinaryTree();
	/*
	    Construct tree
	         4
	       /   \
	      8     3
	     / \   / \
	   10   6 7   2
	       /   \   \
	      9     5  -9
	*/
	// Add tree node
	tree.root = TreeNode(4);
	tree.root!.left = TreeNode(8);
	tree.root!.left!.left = TreeNode(10);
	tree.root!.right = TreeNode(3);
	tree.root!.right!.right = TreeNode(2);
	tree.root!.right!.right!.right = TreeNode(-9);
	tree.root!.right!.left = TreeNode(7);
	tree.root!.right!.left!.right = TreeNode(5);
	tree.root!.left!.right = TreeNode(6);
	tree.root!.left!.right!.left = TreeNode(9);
	// Test
	tree.reverseInorder();
}
main();

input

 Morris Reverse Inorder
   -9   2   3   5   7   4   6   9   8   10
/*
    Kotlin Program
    Reverse inorder traversal using morris traversal
*/
// Binary Tree node
class TreeNode
{
	var data: Int;
	var left: TreeNode ? ;
	var right: TreeNode ? ;
	constructor(data: Int)
	{
		// Set node value
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
class BinaryTree
{
	var root: TreeNode ? ;
	constructor()
	{
		this.root = null;
	}
	// This is display the reverse inorder traversal
	fun reverseInorder(): Unit
	{
		if (this.root == null)
		{
			return;
		}
		// Start to root node of tree
		var current: TreeNode ? = this.root;
		var auxiliary: TreeNode ? ;
		print("\n Morris Reverse Inorder \n");
		while (current != null)
		{
			if (current.right == null)
			{
				// print node value
				print("  " + current.data);
				// When right child are empty then visit to left child
				current = current.left;
			}
			else
			{
				auxiliary = current.right;
				while (auxiliary?.left != null && auxiliary.left != current)
				{
					auxiliary = auxiliary.left;
				}
				if (auxiliary?.left != current)
				{
					// Change link
					auxiliary?.left = current;
					// Get current node is right child
					current = current.right;
				}
				else
				{
					// print node value
					print("  " + current.data);
					// unlink
					auxiliary.left = null;
					// visit to left child
					current = current.left;
				}
			}
		}
	}
}
fun main(args: Array < String > ): Unit
{
	// Create new binary tree
	val tree: BinaryTree = BinaryTree();
	/*
	    Construct tree
	         4
	       /   \
	      8     3
	     / \   / \
	   10   6 7   2
	       /   \   \
	      9     5  -9
	*/
	// Add tree node
	tree.root = TreeNode(4);
	tree.root?.left = TreeNode(8);
	tree.root?.left?.left = TreeNode(10);
	tree.root?.right = TreeNode(3);
	tree.root?.right?.right = TreeNode(2);
	tree.root?.right?.right?.right = TreeNode(-9);
	tree.root?.right?.left = TreeNode(7);
	tree.root?.right?.left?.right = TreeNode(5);
	tree.root?.left?.right = TreeNode(6);
	tree.root?.left?.right?.left = TreeNode(9);
	// Test
	tree.reverseInorder();
}

input

 Morris Reverse Inorder
  -9  2  3  5  7  4  6  9  8  10

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved