public BinaryTree(int rootValue) { root = new Node(rootValue); }
public BinaryTree() { root = null; }
public Node(int data, Node left = null, Node right = null) { this.data = data; this.left = left; this.right = right; }
public Node() { left = null; right = null; data = 0; }
private void DoPrint(Node r) { r.Print(); if (r.Left != null) DoPrint(r.Left); if (r.Right != null) DoPrint(r.Right); }
/// <summary> /// This function scans the tree in a DFS fashion. /// </summary> /// <param name="curr">The current node to look at.</param> /// <param name="target">The number that the branch should add up to.</param> /// <param name="ls">The list to place qualifying sequences into.</param> /// <param name="l">A list containing all previously visited items.</param> private void DoDFS(Node curr, int target, List<List<Node>> ls, List<Node> l) { //First add the current node to the list. if(curr != null) l.Add(curr); //Next, iterate through the list backwards, adding all numbers. //Should the sum equal to the target at any point, we will add //the subset of the list "l" to the sequence list "ls" and continue scanning. //We must always scan the entire list, making the worst case a O(depth). int sum = 0; for (int i = l.Count - 1; i >= 0; i--) { sum += l[i].Data; if (sum == target) { //add sequence to list of sequences ls.Add(l.GetRange(i, (l.Count - i))); } } if (curr.Left != null) DoDFS(curr.Left, target, ls, l); if (curr.Right != null) DoDFS(curr.Right, target, ls, l); }