I have some class.
class TreeNode
{
public TreeNode Left;
public TreeNode Right;
public int Value;
public TreeNode(int i)
{
Value = i;
}
public TreeNode AddLeft(int value)
{
Left = new TreeNode(value); ;
return Left;
}
public TreeNode AddRight(int value)
{
Right = new TreeNode(value);
return Right;
}
public static int GetSum(TreeNode root)
{
if(root.Left == null || root.Right == null) return root.Value;
return root.Value + GetSum(root.Left) + GetSum(root.Right);
}
}
class Program
{
static void Main(string[] args)
{
var root = new TreeNode(10);
var left = root.AddLeft(1);
left.AddLeft(1);
left.AddRight(1);
var right = root.AddRight(1);
right.AddLeft(1);
right.AddRight(1);
int Sum = TreeNode.GetSum(root);
Console.WriteLine("Sum is :{0}", Sum);
Console.ReadKey();
}
}
How to simplify the tree initialization because it is easy to get confused if the tree is big?
something like this:
looks cleaner, what do you think? All you need to do is add a new constructor: