public static void Main()
BinaryTree binaryTree = new BinaryTree();
Node node = binaryTree.Find(5);
int depth = binaryTree.GetTreeDepth();
Console.WriteLine("Tree Depth:");
public Node LeftNode { get; set; }
public Node RightNode { get; set; }
public int Data { get; set; }
public Node Root { get; set; }
public bool Insert(int value)
Node before = null, after = this.Root;
else if (value > after.Data)
Node newNode = new Node();
before.LeftNode = newNode;
before.RightNode = newNode;
public Node Find(int value)
return this.Find(value, this.Root);
private Node Find(int value, Node parent)
if (parent.Data == value)
var leftNode = parent.LeftNode;
var rightNode = parent.RightNode;
var v1 = Find(value, leftNode);
var v2 = Find(value, rightNode);
public int CountLeafNodes()
return this.CountLeafNodes(this.Root, 0);
private int CountLeafNodes(Node parent, int counter)
if (parent.LeftNode == null && parent.RightNode == null)
var leftNode = parent.LeftNode;
var rightNode = parent.RightNode;
int c1 = CountLeafNodes(leftNode, counter);
int c2 = CountLeafNodes(rightNode, counter);
public int GetTreeDepth()
return this.GetTreeDepth(this.Root);
private int GetTreeDepth(Node parent)
return parent == null ? 0 : Math.Max(GetTreeDepth(parent.LeftNode), GetTreeDepth(parent.RightNode)) + 1;