// C# implementation to print the
// path in Boundary Root to Leaf
// Path Traversal.
using System;
using System.Collections;
class GFG{
static int row;
static int count = 0;
// Structure of tree node
public class Node
{
public int key;
public Node left, right;
};
// Utility function to
// create a new node
static Node newNode(int key)
{
Node temp = new Node();
temp.key = key;
temp.left = temp.right = null;
return (temp);
}
// Function to calculate the length
// of longest path of the tree
static int lengthOfLongestPath(Node node)
{
// Base Case
if (node == null)
return 0;
// Recursive call to calculate the
// length of longest path
int left = lengthOfLongestPath(node.left);
int right = lengthOfLongestPath(node.right);
return 1 + (left > right ? left : right);
}
// Function to copy the complete
// path in a matrix
static void copyPath(int[] path, int index,
int[,] mtrx, int r)
{
// Loop to copy the path
for(int i = 0; i < index; i++)
{
mtrx[r, i] = path[i];
}
}
// Function to store all path
// one by one in matrix
static void storePath(Node node, int[] path,
int index, int[,] mtrx)
{
// Base condition
if (node == null)
{
return;
}
// Inserting the current node
// into the current path
path[index] = node.key;
// Recursive call for
// the left sub tree
storePath(node.left, path,
index + 1, mtrx);
// Recursive call for
// the right sub tree
storePath(node.right, path,
index + 1, mtrx);
// Condition to check that current
// node is a leaf node or not
if (node.left == null &&
node.right == null)
{
// Incrementation for changing
// row
row = row + 1;
// Function call for copying
// the path
copyPath(path, index + 1, mtrx, row);
}
}
// Function to calculate
// total path
static int totalPath(Node node)
{
if (node == null)
{
return count;
}
if (node.left == null &&
node.right == null)
{
return count + 1;
}
count = totalPath(node.left);
return totalPath(node.right);
}
// Function for Clockwise Spiral Traversal
// of Binary Tree
static void traverse_matrix(int[,] mtrx,
int height,
int width)
{
int j = 0, k1 = 0, k2 = 0;
int k3 = height - 1;
int k4 = width - 1;
for(int round = 0;
round < height / 2;
round++)
{
for(j = k2; j < width; j++)
{
// Only print those values which
// are not MAX_INTEGER
if (mtrx[k1, j] != 10000000)
{
Console.Write(mtrx[k1, j] + " ");
}
}
Console.WriteLine();
k2 = 0;
k1++;
for(j = k4; j >= 0; j--)
{
// Only print those values which
// are not MAX_INTEGER
if (mtrx[k3, j] != 10000000)
{
Console.Write(mtrx[k3, j] + " ");
}
}
Console.WriteLine();
k4 = width - 1;
k3--;
}
// Condition (one row may be left
// traversing)
// If number of rows in matrix are odd
if (height % 2 != 0)
{
for(j = k2; j < width; j++)
{
// Only print those values which are
// not MAX_INTEGER
if (mtrx[k1, j] != 10000000)
{
Console.Write(mtrx[k1, j] + " ");
}
}
}
}
// Function to print all the paths
// in Boundary Root to Leaf
// Path Traversal
static void PrintPath(Node node)
{
// Calculate the length of
// longest path of the tree
int max_len = lengthOfLongestPath(node);
// Calculate total path
int total_path = totalPath(node);
// Array to store path
int[] path = new int[max_len];
Array.Fill(path, 0);
// Use double pointer to create
// 2D array which will contain
// all path of the tree
int[,] mtrx = new int[total_path, max_len];
// Initialize complete matrix with
// MAX INTEGER(purpose garbage)
for(int i = 0; i < total_path; i++)
{
for(int j = 0; j < max_len; j++)
{
mtrx[i, j] = 10000000;
}
}
row = -1;
storePath(node, path, 0, mtrx);
// Print the circular clockwise spiral
// traversal of the tree
traverse_matrix(mtrx, total_path, max_len);
}
// Driver Code
public static void Main(string[] args)
{
/* 10
/ \
13 11
/ \
19 23
/ \ / \
21 29 43 15
/
7 */
// Create Binary Tree as shown
Node root = newNode(10);
root.left = newNode(13);
root.right = newNode(11);
root.right.left = newNode(19);
root.right.right = newNode(23);
root.right.left.left = newNode(21);
root.right.left.right = newNode(29);
root.right.right.left = newNode(43);
root.right.right.right = newNode(15);
root.right.right.right.left = newNode(7);
// Function Call
PrintPath(root);
}
}
// This code is contributed by rutvik_56