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("---------------");
printLeftView(n);
}
public static void printLeftView(BSTnode n)
{
List<BSTnode> li = null;
Stack<List<BSTnode>> s = new Stack<List<BSTnode>>();
if(n!=null)
{
s.Push(new List<BSTnode>() { n});
}
do
{
li = new List<BSTnode>();
foreach (var item in s.Peek())
{
if (item.LeftNode != null)
{
li.Add(item.LeftNode);
}
if (item.rightNode != null)
{
li.Add(item.rightNode);
}
}
if(li.Count>0)
s.Push(li);
} while (li.Count > 0);
li = new List<BSTnode>();
foreach (var item in s)
{
li.Add(item.First());
}
li.Reverse();
foreach (var item in li)
{
Console.WriteLine(item.data);
}
}
}
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