// Depth First Options public static List<LinkedList<TreeNode>> CreateLevelLinkedList(TreeNode root) { var output = new List<LinkedList<TreeNode>>(); CreateLevelLinkedList(root, output, 0); // might need to pass by reference return output; }
public static int lastPrinted = int.MaxValue; // can switch to pass by reference #endregion Fields #region Methods public static bool CheckForBinarySearchTreeByMinMax(TreeNode n, int min, int max) { if (n == null) return true; if (n.Data <= min || n.Data > max) return false; if (!CheckForBinarySearchTreeByMinMax(n.Left, min, n.Data) || !CheckForBinarySearchTreeByMinMax(n.Right, n.Data, max)) return false; return true; }
public static TreeNode CreateMinimalBST(int[] input, int start, int end) { if (end < start) return null; int mid = (start + end)/2; TreeNode n = new TreeNode(input[mid]); n.Left = CreateMinimalBST(input, start, mid - 1); n.Right = CreateMinimalBST(input, mid + 1, end); return n; }
// Runs in O(N^2) (quadratic) time public static bool IsBalanced(TreeNode root) { if (root == null) return true; // base case var heightDiff = GetHeight(root.Left) - GetHeight(root.Right); if (Math.Abs(heightDiff) > 1) { return false; } else { return IsBalanced(root.Left) && IsBalanced(root.Right); // recurse } }
public static bool CheckForBinarySearchTreeInOrder(TreeNode n) { if (n == null) return true; // check / recursive left if (!CheckForBinarySearchTreeInOrder(n.Left)) return false; // check current if (n.Data < lastPrinted) return false; lastPrinted = n.Data; // check / recursive right if (!CheckForBinarySearchTreeInOrder(n.Right)) return false; return true; // all good! }
public static int CheckHeight(TreeNode root) { if (root == null) return 0; // height of 0 // check if left is balanced var leftHeight = CheckHeight(root.Left); if (leftHeight == -1) return -1; // not balanced // check if right is balanced var rightHeight = CheckHeight(root.Right); if (rightHeight == -1) return -1; // not balanced // check if current node is balanced var heightDiff = leftHeight - rightHeight; if (Math.Abs(heightDiff) > 1) return -1; return Math.Max(leftHeight, rightHeight) + 1; }
private static void CreateLevelLinkedList(TreeNode root, List<LinkedList<TreeNode>> lists, int level) { if (root == null) return; // base case LinkedList<TreeNode> list; if (lists.Count == level) // level not contained in the list yet { /* * Levels are always traversed in order. So, if this is the first time we've visited level i, * we must have seen levels 0 through i - 1. We can therefore safely add the level at the end */ list = new LinkedList<TreeNode>(); lists.Add(list); } else { list = lists[level]; } list.AddLast(root); CreateLevelLinkedList(root.Left, lists, level + 1); CreateLevelLinkedList(root.Right, lists, level + 1); }
// Breadth First Options public static List<LinkedList<TreeNode>> CreateLevelLinkedListBreadthFirst(TreeNode root) { var result = new List<LinkedList<TreeNode>>(); // visit the root var current = new LinkedList<TreeNode>(); if (root != null) current.AddLast(root); while (current.Count > 0) { result.Add(current); // add the previous level LinkedList<TreeNode> parents = current; // go to the next level current = new LinkedList<TreeNode>(); foreach (var parent in parents) { // visit the children if (parent.Left != null) current.AddLast(parent.Left); if (parent.Right != null) current.AddLast(parent.Right); } } return result; }
// Runs in O(N) time (linear) and O(log N) space public static bool IsBalancedLinearAlgorithm(TreeNode root) { return CheckHeight(root) != -1; }
public static int GetHeight(TreeNode root) { if (root == null) return 0; // base case return Math.Max(GetHeight(root.Left), GetHeight(root.Right)) + 1; }