Efficiently print all paths in binary tree using recursion

Here given code implementation process.

/*
  Java Program for 
  Efficiently print all paths in binary tree using recursion
*/
// 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;
	}
}
public class BinaryTree
{
	public TreeNode root;
	public BinaryTree()
	{
		//Set root of tree
		this.root = null;
	}
	void printPath(TreeNode node, String path)
	{
		if (node == null)
		{
			return;
		}
		else if (node.left == null && node.right == null)
		{
			// Display path 
			System.out.println(path + " " + node.data);
		}
		else
		{
			// Visit left subtree
			printPath(node.left, path + " " + node.data);
			// Visit left subtree
			printPath(node.right, path + " " + node.data);
		}
	}
	public static void main(String[] args)
	{
		BinaryTree tree = new BinaryTree();
		/*
		            
		        1
		      /   \
		     5     7
		    / \   / \
		   2   4 9   3  
		      /       \
		     6         8
		-------------------
		    Binary Tree
		*/
		tree.root = new TreeNode(1);
		tree.root.left = new TreeNode(5);
		tree.root.left.right = new TreeNode(4);
		tree.root.left.left = new TreeNode(2);
		tree.root.left.right.left = new TreeNode(6);
		tree.root.right = new TreeNode(7);
		tree.root.right.left = new TreeNode(9);
		tree.root.right.right = new TreeNode(3);
		tree.root.right.right.right = new TreeNode(8);
		// Test
		tree.printPath(tree.root, "");
	}
}

input

 1 5 2
 1 5 4 6
 1 7 9
 1 7 3 8
// Include header file
#include <iostream>

using namespace std;
/*
  C++ Program for 
  Efficiently print all paths in binary tree using recursion
*/
// 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;
	}
	void printPath(TreeNode *node, string path)
	{
		if (node == NULL)
		{
			return;
		}
		else
		{
			if (node->left == NULL && node->right == NULL)
			{
				// Display path
				cout << path << " " << node->data << endl;
			}
			else
			{
				// Visit left subtree
				this->printPath(node->left, path  +  " "
					 +  to_string(node->data));
				// Visit left subtree
				this->printPath(node->right, path  +  " "
					 +  to_string(node->data));
			}
		}
	}
};
int main()
{
	BinaryTree *tree = new BinaryTree();
	/*
	        1
	      /   \
	     5     7
	    / \   / \
	   2   4 9   3  
	      /       \
	     6         8
	-------------------
	    Binary Tree
	*/
	tree->root = new TreeNode(1);
	tree->root->left = new TreeNode(5);
	tree->root->left->right = new TreeNode(4);
	tree->root->left->left = new TreeNode(2);
	tree->root->left->right->left = new TreeNode(6);
	tree->root->right = new TreeNode(7);
	tree->root->right->left = new TreeNode(9);
	tree->root->right->right = new TreeNode(3);
	tree->root->right->right->right = new TreeNode(8);
	// Test
	tree->printPath(tree->root, "");
	return 0;
}

input

 1 5 2
 1 5 4 6
 1 7 9
 1 7 3 8
// Include namespace system
using System;
/*
  Csharp Program for 
  Efficiently print all paths in binary tree using recursion
*/
// 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 root of tree
		this.root = null;
	}
	void printPath(TreeNode node, String path)
	{
		if (node == null)
		{
			return;
		}
		else
		{
			if (node.left == null && node.right == null)
			{
				// Display path
				Console.WriteLine(path + " " + node.data);
			}
			else
			{
				// Visit left subtree
				this.printPath(node.left, path + " " + node.data);
				// Visit left subtree
				this.printPath(node.right, path + " " + node.data);
			}
		}
	}
	public static void Main(String[] args)
	{
		BinaryTree tree = new BinaryTree();
		/*
		        1
		      /   \
		     5     7
		    / \   / \
		   2   4 9   3  
		      /       \
		     6         8
		-------------------
		    Binary Tree
		*/
		tree.root = new TreeNode(1);
		tree.root.left = new TreeNode(5);
		tree.root.left.right = new TreeNode(4);
		tree.root.left.left = new TreeNode(2);
		tree.root.left.right.left = new TreeNode(6);
		tree.root.right = new TreeNode(7);
		tree.root.right.left = new TreeNode(9);
		tree.root.right.right = new TreeNode(3);
		tree.root.right.right.right = new TreeNode(8);
		// Test
		tree.printPath(tree.root, "");
	}
}

input

 1 5 2
 1 5 4 6
 1 7 9
 1 7 3 8
<?php
/*
  Php Program for 
  Efficiently print all paths in binary tree using recursion
*/
// 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;
	}

	function printPath($node, $path)
	{
		if ($node == NULL)
		{
			return;
		}
		else
		{
			if ($node->left == NULL && $node->right == NULL)
			{
				// Display path
				echo $path." ".strval($node->data)."\n";
			}
			else
			{
				// Visit left subtree
				$this->printPath($node->left, $path.
					" ".strval($node->data));
				// Visit left subtree
				$this->printPath($node->right, $path.
					" ".strval($node->data));
			}
		}
	}
}

function main()
{
	$tree = new BinaryTree();
	/*
	        1
	      /   \
	     5     7
	    / \   / \
	   2   4 9   3  
	      /       \
	     6         8
	-------------------
	    Binary Tree
	*/
	$tree->root = new TreeNode(1);
	$tree->root->left = new TreeNode(5);
	$tree->root->left->right = new TreeNode(4);
	$tree->root->left->left = new TreeNode(2);
	$tree->root->left->right->left = new TreeNode(6);
	$tree->root->right = new TreeNode(7);
	$tree->root->right->left = new TreeNode(9);
	$tree->root->right->right = new TreeNode(3);
	$tree->root->right->right->right = new TreeNode(8);
	// Test
	$tree->printPath($tree->root, "");
}
main();

input

 1 5 2
 1 5 4 6
 1 7 9
 1 7 3 8
/*
  Node JS Program for 
  Efficiently print all paths in binary tree using recursion
*/
// 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;
	}
	printPath(node, path)
	{
		if (node == null)
		{
			return;
		}
		else
		{
			if (node.left == null && node.right == null)
			{
				// Display path
				console.log(path + " " + node.data);
			}
			else
			{
				// Visit left subtree
				this.printPath(node.left, path + " " + node.data);
				// Visit left subtree
				this.printPath(node.right, path + " " + node.data);
			}
		}
	}
}

function main()
{
	var tree = new BinaryTree();
	/*
	        1
	      /   \
	     5     7
	    / \   / \
	   2   4 9   3  
	      /       \
	     6         8
	-------------------
	    Binary Tree
	*/
	tree.root = new TreeNode(1);
	tree.root.left = new TreeNode(5);
	tree.root.left.right = new TreeNode(4);
	tree.root.left.left = new TreeNode(2);
	tree.root.left.right.left = new TreeNode(6);
	tree.root.right = new TreeNode(7);
	tree.root.right.left = new TreeNode(9);
	tree.root.right.right = new TreeNode(3);
	tree.root.right.right.right = new TreeNode(8);
	// Test
	tree.printPath(tree.root, "");
}
main();

input

 1 5 2
 1 5 4 6
 1 7 9
 1 7 3 8
#  Python 3 Program for 
#  Efficiently print all paths in binary tree using recursion

#  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
	
	def printPath(self, node, path) :
		if (node == None) :
			return
		else :
			if (node.left == None and node.right == None) :
				#  Display path
				print(path , node.data)
			else :
				#  Visit left subtree
				self.printPath(node.left, path + " "
					+ str(node.data))
				#  Visit left subtree
				self.printPath(node.right, path + " "
					+ str(node.data))
			
		
	

def main() :
	tree = BinaryTree()
	#        1
	#      /   \
	#     5     7
	#    / \   / \
	#   2   4 9   3  
	#      /       \
	#     6         8
	# -------------------
	#    Binary Tree
	tree.root = TreeNode(1)
	tree.root.left = TreeNode(5)
	tree.root.left.right = TreeNode(4)
	tree.root.left.left = TreeNode(2)
	tree.root.left.right.left = TreeNode(6)
	tree.root.right = TreeNode(7)
	tree.root.right.left = TreeNode(9)
	tree.root.right.right = TreeNode(3)
	tree.root.right.right.right = TreeNode(8)
	#  Test
	tree.printPath(tree.root, "")

if __name__ == "__main__": main()

input

 1 5 2
 1 5 4 6
 1 7 9
 1 7 3 8
#  Ruby Program for 
#  Efficiently print all paths in binary tree using recursion

#  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

	def printPath(node, path) 
		if (node == nil) 
			return
		else 
			if (node.left == nil && node.right == nil) 
				#  Display path
				print(path ," ", node.data, "\n")
			else 
				#  Visit left subtree
				self.printPath(node.left, path + " " + node.data.to_s)
				#  Visit left subtree
				self.printPath(node.right, path + " " + node.data.to_s)
			end

		end

	end

end

def main() 
	tree = BinaryTree.new()
	#        1
	#      /   \
	#     5     7
	#    / \   / \
	#   2   4 9   3  
	#      /       \
	#     6         8
	# -------------------
	#    Binary Tree
	tree.root = TreeNode.new(1)
	tree.root.left = TreeNode.new(5)
	tree.root.left.right = TreeNode.new(4)
	tree.root.left.left = TreeNode.new(2)
	tree.root.left.right.left = TreeNode.new(6)
	tree.root.right = TreeNode.new(7)
	tree.root.right.left = TreeNode.new(9)
	tree.root.right.right = TreeNode.new(3)
	tree.root.right.right.right = TreeNode.new(8)
	#  Test
	tree.printPath(tree.root, "")
end

main()

input

 1 5 2
 1 5 4 6
 1 7 9
 1 7 3 8
/*
  Scala Program for 
  Efficiently print all paths in binary tree using recursion
*/
// 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);
	}
	def printPath(node: TreeNode, path: String): Unit = {
		if (node == null)
		{
			return;
		}
		else
		{
			if (node.left == null && node.right == null)
			{
				// Display path
				println(path + " " + node.data);
			}
			else
			{
				// Visit left subtree
				printPath(node.left, path + " " + node.data.toString());
				// Visit left subtree
				printPath(node.right, path + " " + node.data.toString());
			}
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var tree: BinaryTree = new BinaryTree();
		/*
		        1
		      /   \
		     5     7
		    / \   / \
		   2   4 9   3  
		      /       \
		     6         8
		-------------------
		    Binary Tree
		*/
		tree.root = new TreeNode(1);
		tree.root.left = new TreeNode(5);
		tree.root.left.right = new TreeNode(4);
		tree.root.left.left = new TreeNode(2);
		tree.root.left.right.left = new TreeNode(6);
		tree.root.right = new TreeNode(7);
		tree.root.right.left = new TreeNode(9);
		tree.root.right.right = new TreeNode(3);
		tree.root.right.right.right = new TreeNode(8);
		// Test
		tree.printPath(tree.root, "");
	}
}

input

 1 5 2
 1 5 4 6
 1 7 9
 1 7 3 8
/*
  Swift 4 Program for 
  Efficiently print all paths in binary tree using recursion
*/
// 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;
	}
	func printPath(_ node: TreeNode? , _ path : String)
	{
		if (node == nil)
		{
			return;
		}
		else
		{
			if (node!.left == nil && node!.right == nil)
			{
				// Display path
				print(path , node!.data);
			}
			else
			{
				// Visit left subtree
				self.printPath(node!.left, path + " "
					+ String(node!.data));
				// Visit left subtree
				self.printPath(node!.right, path + " "
					+ String(node!.data));
			}
		}
	}
}
func main()
{
	let tree: BinaryTree = BinaryTree();
	/*
	        1
	      /   \
	     5     7
	    / \   / \
	   2   4 9   3  
	      /       \
	     6         8
	-------------------
	    Binary Tree
	*/
	tree.root = TreeNode(1);
	tree.root!.left = TreeNode(5);
	tree.root!.left!.right = TreeNode(4);
	tree.root!.left!.left = TreeNode(2);
	tree.root!.left!.right!.left = TreeNode(6);
	tree.root!.right = TreeNode(7);
	tree.root!.right!.left = TreeNode(9);
	tree.root!.right!.right = TreeNode(3);
	tree.root!.right!.right!.right = TreeNode(8);
	// Test
	tree.printPath(tree.root, "");
}
main();

input

 1 5 2
 1 5 4 6
 1 7 9
 1 7 3 8
/*
  Kotlin Program for 
  Efficiently print all paths in binary tree using recursion
*/
// 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;
	}
	fun printPath(node: TreeNode ? , path : String): Unit
	{
		if (node == null)
		{
			return;
		}
		else
		{
			if (node.left == null && node.right == null)
			{
				// Display path
				println(path + " " + node.data.toString());
			}
			else
			{
				// Visit left subtree
				this.printPath(node.left, path + " " + node.data.toString());
				// Visit left subtree
				this.printPath(node.right, path + " " + node.data.toString());
			}
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val tree: BinaryTree = BinaryTree();
	/*
	        1
	      /   \
	     5     7
	    / \   / \
	   2   4 9   3  
	      /       \
	     6         8
	-------------------
	    Binary Tree
	*/
	tree.root = TreeNode(1);
	tree.root?.left = TreeNode(5);
	tree.root?.left?.right = TreeNode(4);
	tree.root?.left?.left = TreeNode(2);
	tree.root?.left?.right?.left = TreeNode(6);
	tree.root?.right = TreeNode(7);
	tree.root?.right?.left = TreeNode(9);
	tree.root?.right?.right = TreeNode(3);
	tree.root?.right?.right?.right = TreeNode(8);
	// Test
	tree.printPath(tree.root, "");
}

input

 1 5 2
 1 5 4 6
 1 7 9
 1 7 3 8


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