using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
public TreeNode left_ptr;
public TreeNode right_ptr;
public TreeNode(int val){
public int parentNodeIndex;
public int childNodeIndex;
public string leftRightFlag;
private List<int> nodeValues;
private List<Edge> edges;
this.nodeValues = new List<int>();
this.edges = new List<Edge>();
public void readRawValues(){
noOfNodes = Int32.Parse(Console.ReadLine());
string[] nodeValuesStringArray = Console.ReadLine().Split(' ');
for(int i=0; i<noOfNodes; i++){
int val = Int32.Parse(nodeValuesStringArray[i]);
rootIndex = Int32.Parse(Console.ReadLine());
noOfEdges = Int32.Parse(Console.ReadLine());
for(int i=0; i<noOfEdges; i++){
string[] edgeStringArray = Console.ReadLine().Split(' ');
edge.parentNodeIndex = Int32.Parse(edgeStringArray[0]);
edge.childNodeIndex = Int32.Parse(edgeStringArray[1]);
edge.leftRightFlag = edgeStringArray[2];
public void buildFromRawValues(){
List<TreeNode> nodes = new List<TreeNode>();
for(int i=0; i<noOfNodes; i++){
nodes.Add(new TreeNode(nodeValues[i]));
for(int i=0; i<noOfEdges; i++){
if(edges[i].leftRightFlag=="L"){
nodes[edges[i].parentNodeIndex].left_ptr = nodes[edges[i].childNodeIndex];
nodes[edges[i].parentNodeIndex].right_ptr = nodes[edges[i].childNodeIndex];
public static TreeNode readBinaryTree(){
BinaryTree inputBinaryTree = new BinaryTree();
inputBinaryTree.readRawValues();
inputBinaryTree.buildFromRawValues();
return inputBinaryTree.root;
public static int maxLeftNodeVal = -10^9;
public static int minRightNodeVal = 10^9;
public static bool isBST(TreeNode root)
return isBSTHelper(root, minRightNodeVal, maxLeftNodeVal);
public static bool isBSTHelper(TreeNode root,int min, int max){
if(root.val < min || root.val > max)
return (isBSTHelper(root.left_ptr, min, root.val) && isBSTHelper(root.right_ptr, root.val, max));
public static void BFS(TreeNode root)
Queue<TreeNode> q = new Queue<TreeNode>();