internal void DiagonalTraversalofBinaryTree(BinaryTreeNodeSM root, int v, Dictionary <int, HashSet <int> > keyValuePairs) { if (root != null) { if (keyValuePairs.ContainsKey(v)) { keyValuePairs[v].Add(root.data); } else { keyValuePairs.Add(v, new HashSet <int>() { root.data }); } if (root.leftChild != null) { DiagonalTraversalofBinaryTree(root.leftChild, v + 1, keyValuePairs); } if (root.rightChild != null) { DiagonalTraversalofBinaryTree(root.rightChild, v, keyValuePairs); } } }
internal int FindHeightIterativelySm(BinaryTreeNodeSM root) { int height = 0; Queue queue = new Queue(); queue.Enqueue(root); while (true) { int queueSize = queue.Count; if (queueSize > 0) { height += 1; } else { break; } while (queueSize > 0) { BinaryTreeNodeSM binaryTreeNodeSM = (BinaryTreeNodeSM)queue.Dequeue(); if (binaryTreeNodeSM.leftChild != null) { queue.Enqueue(binaryTreeNodeSM.leftChild); } if (binaryTreeNodeSM.rightChild != null) { queue.Enqueue(binaryTreeNodeSM.rightChild); } queueSize--; } } return(height); }
public int MaximumWidthOfTreeUsingQueue(BinaryTreeNodeSM root) { if (root == null) { return(0); } Queue <BinaryTreeNodeSM> queu = new Queue <BinaryTreeNodeSM>(); queu.Enqueue(root); int result = 0; while (queu.Count > 0) { int s = queu.Count(); if (s > result) { result = s; } while (s-- > 0) { BinaryTreeNodeSM b = queu.Dequeue(); if (b.leftChild != null) { queu.Enqueue(b.leftChild); } if (b.rightChild != null) { queu.Enqueue(b.rightChild); } } } return(result); }
internal bool CheckSumofCoveredUncoveredNodes(BinaryTreeNodeSM root) { int sumUc = GetUncoveredSum(root); int total = GetSum(root); return(sumUc == (total - sumUc)); }
internal void LevelOrderTransversalLineByLineSM(BinaryTreeNodeSM root) { Queue <BinaryTreeNodeSM> q = new Queue <BinaryTreeNodeSM>(); q.Enqueue(root); while (q.Count > 0) { int count = q.Count; while (count != 0) { BinaryTreeNodeSM binaryTreeNodeSM = q.Dequeue(); Console.Write(binaryTreeNodeSM.data); if (binaryTreeNodeSM.leftChild != null) { q.Enqueue(binaryTreeNodeSM.leftChild); } if (binaryTreeNodeSM.rightChild != null) { q.Enqueue(binaryTreeNodeSM.rightChild); } count--; } Console.WriteLine(); } }
private int GetSum(BinaryTreeNodeSM root) { if (root == null) { return(0); } return(root.data + GetSum(root.leftChild) + GetSum(root.rightChild)); }
internal void PrintRootToLeafWithoutRecursion(BinaryTreeNodeSM root, Stack <int> s) { if (root == null) { return; } s.Push(root.data); }
public void BreadthFirstTransversal(BinaryTreeNodeSM node) { int height = HeightSM(node); for (int i = 1; i <= height; i++) { PrintLevel(i, node); } }
public void PostOrderTransversal(BinaryTreeNodeSM node) { if (node == null) { return; } PostOrderTransversal(node.leftChild); PostOrderTransversal(node.rightChild); Console.WriteLine(node.data); }
internal int SumOfLeftLeaves(BinaryTreeNodeSM root) { if (root == null) { return(0); } int sum = 0; sum = SumOfLeftLeavesUtil(root, false); return(sum); }
internal int FindHeightSm(BinaryTreeNodeSM root) { if (root == null) { return(0); } int lHeight = FindHeightSm(root.leftChild); int rHeight = FindHeightSm(root.rightChild); return(1 + Math.Max(lHeight, rHeight)); }
internal void PopulateNextInOrderSuccessorNonStaticSM(BinaryTreeNodeSM root, BinaryTreeNodeSM next) { if (root == null) { return; } PopulateNextInOrderSuccessorNonStaticSM(root.rightChild, next); root.inorderSuccessorNext = next; next = root; PopulateNextInOrderSuccessorNonStaticSM(root.leftChild, next); }
internal void PopulateNextInOrderSuccessorSM(BinaryTreeNodeSM root) { if (root == null) { return; } PopulateNextInOrderSuccessorSM(root.rightChild); root.inorderSuccessorNext = nextInOrderSuccess; nextInOrderSuccess = root; PopulateNextInOrderSuccessorSM(root.leftChild); }
internal void BoundaryTraversal(BinaryTreeNodeSM root) { if (root == null) { return; } PrintLeftSide(root); PrintLeaves(root.leftChild); PrintLeaves(root.rightChild); PrintRightSide(root.rightChild); }
private int SumOfLeftLeavesUtil(BinaryTreeNodeSM root, bool isLeft) { if (root == null) { return(0); } if (isLeft && root.leftChild == null && root.rightChild == null) { return(root.data); } return(SumOfLeftLeavesUtil(root.leftChild, true) + SumOfLeftLeavesUtil(root.rightChild, false)); }
private int WidthSM(int i, BinaryTreeNodeSM root) { if (root == null) { return(0); } if (i == 1) { return(1); } return(WidthSM(i - 1, root.leftChild) + WidthSM(i - 1, root.rightChild)); }
public int DiameterOfBinaryTree(BinaryTreeNodeSM node) { if (node == null) { return(0); } int lheight = HeightSM(node.leftChild); int rheight = HeightSM(node.rightChild); int ldiameter = DiameterOfBinaryTree(node.leftChild); int rDiameter = DiameterOfBinaryTree(node.rightChild); return(Math.Max(1 + lheight + rheight, Math.Max(ldiameter, rDiameter))); }
public void LevelOrderTransversal(BinaryTreeNodeSM node) { if (node == null) { return; } int height = HeightSM(node); for (int i = 1; i <= height; i++) { PrintLevel(i, node); } }
private int GetUncoveredSum(BinaryTreeNodeSM root) { if (root == null) { return(0); } int leftSum = 0; int rightSum = 0; GetLeftUncovered(root.leftChild, ref leftSum); GetRightUncovered(root.rightChild, ref rightSum); return(root.data + leftSum + rightSum); }
internal bool IsBinaryTreeSumTreeFaster(BinaryTreeNodeSM root) { if (root == null) { return(true); } if (root.leftChild == null && root.rightChild == null) { return(true); } if (IsBinaryTreeSumTreeFaster(root.leftChild) && IsBinaryTreeSumTreeFaster(root.rightChild)) { int leftData = 0; int rightChild = 0; if (root.leftChild == null) { leftData = 0; } else if (isLeaf(root.leftChild)) { leftData = root.leftChild.data; } else { leftData = root.leftChild.data * 2; } if (root.rightChild == null) { rightChild = 0; } else if (isLeaf(root.rightChild)) { rightChild = root.rightChild.data; } else { rightChild = root.rightChild.data * 2; } if (root.data == leftData + rightChild) { return(true); } else { return(false); } } return(false); }
private void PrintLeaves(BinaryTreeNodeSM node) { if (node == null) { return; } PrintLeaves(node.leftChild); if (node.leftChild == null && node.rightChild == null) { Console.WriteLine(node.data); } PrintLeaves(node.rightChild); }
public void PrintNodeAtKDistance(int k, BinaryTreeNodeSM root) { if (root == null) { return; } if (k == 1) { Console.WriteLine(root.data); return; } PrintNodeAtKDistance(k - 1, root.leftChild); PrintNodeAtKDistance(k - 1, root.rightChild); }
internal void FindNthNodeInpostTransversal(BinaryTreeNodeSM root, ref int n) { if (root == null) { return; } FindNthNodeInpostTransversal(root.leftChild, ref n); FindNthNodeInpostTransversal(root.rightChild, ref n); n--; if (n == 0) { Console.WriteLine(root.data); } }
internal bool CheckForFullBinaryTree(BinaryTreeNodeSM root) { if (root == null) { return(true); } if (root.leftChild == null && root.rightChild == null) { return(true); } if (root.leftChild == null || root.rightChild == null) { return(false); } return(CheckForFullBinaryTree(root.leftChild) && CheckForFullBinaryTree(root.rightChild)); }
internal bool IsBinaryTreeSumTree(BinaryTreeNodeSM root) { if (root == null) { return(true); } if (root.leftChild == null && root.rightChild == null) { return(true); } int leftSum = FindSum(root.leftChild); int rightSum = FindSum(root.rightChild); return(root.data == leftSum + rightSum && IsBinaryTreeSumTree(root.leftChild) && IsBinaryTreeSumTree(root.rightChild)); }
public void InOrderTreeTransversalWithoutRecursion(BinaryTreeNodeSM root) { Stack <BinaryTreeNodeSM> s = new Stack <BinaryTreeNodeSM>(); while (root != null || s.Count > 0) { while (root != null) { s.Push(root); root = root.leftChild; } root = s.Pop(); Console.WriteLine(root.data); root = root.rightChild; } }
internal bool ChildrenSumProperty(BinaryTreeNodeSM root) { if (root == null) { return(true); } if (root.leftChild == null && root.rightChild == null) { return(true); } int leftData = root.leftChild != null ? root.leftChild.data : 0; int rightData = root.rightChild != null ? root.rightChild.data : 0; return(root.data == leftData + rightData && ChildrenSumProperty(root.rightChild) && ChildrenSumProperty(root.leftChild)); }
internal void SumOfParentNodes(BinaryTreeNodeSM root, int data, ref int sum) { if (root == null) { return; } if (root.leftChild == null && root.rightChild == null) { return; } if (root.leftChild.data == data || root.rightChild.data == data) { sum = sum + root.data; } SumOfParentNodes(root.leftChild, data, ref sum); SumOfParentNodes(root.rightChild, data, ref sum); }
private void PrintLeftSide(BinaryTreeNodeSM root) { if (root == null) { return; } if (root.leftChild != null) { Console.WriteLine(root.data); PrintLeftSide(root.leftChild); } else if (root.rightChild != null) { Console.WriteLine(root.data); PrintLeftSide(root.rightChild); } }
private void PrintLevel(int level, BinaryTreeNodeSM node) { if (node == null) { return; } if (level == 1) { Console.WriteLine(node.data); return; } else { level--; PrintLevel(level, node.leftChild); PrintLevel(level, node.rightChild); } }