We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
public class Node {
public int data;
public Node left;
public Node right;
public Node(int d)
{
data = data;
}
}
public static Node insert(Node root, int data) {
if(root == null) {
return new Node(data);
} else {
Node cur;
if(data <= root.data) {
cur = insert(root.left, data);
root.left = cur;
} else {
cur = insert(root.right, data);
root.right = cur;
}
return root;
}
}
static void preOrder(Node root)
{
// Console.WriteLine(x);
}
static void Main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
string input = Console.ReadLine();
int nodeCount = Convert.ToInt32(input);
Console.WriteLine($"Nodes: {nodeCount}");
string[] nodes = Console.ReadLine().Split(" ");
Node root;
foreach(string x in nodes)
{
int data = Convert.ToInt32(x);
Node insert(root, data);
}
preOrder(root);
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Tree: Preorder Traversal
You are viewing a single comment's thread. Return to all comments →
Here is the C# 'frame' of the question:
class Solution {
}