Skip to content

Commit ee13a39

Browse files
authored
Create FindMinimum&MaximumBinaryTree.cs
1 parent 658215f commit ee13a39

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

FindMinimum&MaximumBinaryTree.cs

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
3+
namespace ConsoleApplication12
4+
{
5+
public class Node
6+
{
7+
public readonly int Data;
8+
public Node Right, Left;
9+
10+
public Node(int key)
11+
{
12+
Data = key;
13+
Right = Left = null;
14+
}
15+
}
16+
17+
public class BinaryTree
18+
{
19+
public Node Root;
20+
21+
public int FindMax(Node node)
22+
{
23+
if (node == null)
24+
return int.MinValue;
25+
var result = node.Data;
26+
var leftResult = FindMax(node.Left);
27+
var rightResult = FindMax(node.Right);
28+
29+
if (leftResult > result)
30+
result = leftResult;
31+
if (rightResult > result)
32+
result = rightResult;
33+
return result;
34+
}
35+
36+
public int FindMin(Node node)
37+
{
38+
if (node == null)
39+
return int.MaxValue;
40+
var result = node.Data;
41+
var leftResult = FindMax(node.Left);
42+
var rightResult = FindMax(node.Right);
43+
44+
if (leftResult < result)
45+
result = leftResult;
46+
if (rightResult < result)
47+
result = rightResult;
48+
return result;
49+
}
50+
}
51+
52+
internal static class Program
53+
{
54+
public static void Main(string[] args)
55+
{
56+
var tree = new BinaryTree {Root = new Node(2) {Left = new Node(7), Right = new Node(5)}};
57+
tree.Root.Left.Right = new Node(6) {Left = new Node(1), Right = new Node(11)};
58+
tree.Root.Right.Right = new Node(9) {Left = new Node(4)};
59+
60+
Console.WriteLine("Maximum Number in Binary Tree : " + tree.FindMax(tree.Root));
61+
Console.WriteLine("Minimum Number in Binary Tree : " + tree.FindMin(tree.Root));
62+
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)