Here is the Code:
class Program
{
static void Main(string[] args)
{
// This Solution is written by me and is not completely tested. Please comment if you found any alternative solutions or any enhancement to my solution.
BSTnode n = new BSTnode(5);
n.add(2);
n.add(7);
n.add(3);
n.add(1);
n.add(6);
n.add(8);
n.add(9);
Console.WriteLine("---------------");
SpiralOrderBT(n);
}
public static void SpiralOrderBT(BSTnode node)
{
List<List<BSTnode>> nodes = new List<List<BSTnode>>();
List<BSTnode> li = new List<BSTnode>();
li.Add(node);
nodes.Add(li);
do
{
li = new List<BSTnode>();
foreach (var item in nodes.Last())
{
if (item.LeftNode != null)
{
li.Add(item.LeftNode);
}
if (item.rightNode != null)
{
li.Add(item.rightNode);
}
}
if(li.Count > 0){
nodes.Add(li);
}
} while (li.Count > 0);
int count = 0;
foreach (var item in nodes)
{
if (count % 2 == 0)
{
item.Reverse();
}
foreach (var i in item)
{
Console.Write(i.data);
}
Console.WriteLine();
count++;
}
}
}
Popular Posts
-
Here is the code: // => Brute Force Solution: O(m* n) static long arrayManipulation(int n, int[][] queries) { ...
-
Whatever it is one fine day everyone on this planet who are born have to die for sure. When you are close ones are with you, you wont know ...
-
HTML: <div class="outer-container"> <div class="inner-container"> <div class="t...
No comments:
Post a Comment