Last active
December 3, 2016 05:31
-
-
Save mataprasad/9d724a23fdfafc46f70d0d0cf8ebc7e4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//https://www.hackerearth.com/practice/data-structures/trees/binary-and-nary-trees/tutorial/ | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
namespace Treeimpl | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var line1 = Console.ReadLine().Trim().Split(' ').Select(int.Parse).ToList(); | |
int T = line1[0]; | |
int X = line1[1]; | |
Tree tree = new Tree(); | |
tree.Childs = new Node[T]; | |
tree.Childs[0] = new Node() { Data = X }; | |
for (int i = 1; i < 2 * (T - 1); i++) | |
{ | |
string LR = Console.ReadLine().Trim(); | |
int V = int.Parse(Console.ReadLine().Trim()); | |
for (int j = 0; j < LR.Length; j++) | |
{ | |
if (LR[j] == 'L') | |
{ | |
if (tree.Childs[j].IndexL == -1) | |
{ | |
tree.Childs[j].IndexL = i; | |
tree.Childs[i] = new Node() { Data = V, IndexP = j }; | |
break; | |
} | |
else | |
{ | |
j = tree.Childs[j].IndexL - 1; | |
} | |
} | |
if (LR[j] == 'R') | |
{ | |
if (tree.Childs[j].IndexR == -1) | |
{ | |
tree.Childs[j].IndexR = i; | |
tree.Childs[i] = new Node() { Data = V, IndexP = j }; | |
break; | |
} | |
else | |
{ | |
j = tree.Childs[j].IndexR - 1; | |
} | |
} | |
} | |
} | |
} | |
} | |
public class Node | |
{ | |
public Node() | |
{ | |
Data = null; | |
IndexL = -1; | |
IndexR = -1; | |
IndexP = -1; | |
} | |
public object Data { get; set; } | |
public int IndexL { get; set; } | |
public int IndexR { get; set; } | |
public int IndexP { get; set; } | |
public bool HasNode() | |
{ | |
if (IndexL == -1 && IndexR == -1) | |
{ | |
return true; | |
} | |
return false; | |
} | |
} | |
public class Tree | |
{ | |
public Node[] Childs { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment