public void BSTFromPreorderTraversalM() { int[] preOrderTraversal = { 10, 5, 1, 7, 40, 50 }; BSTFromPreorderTraversalC bstFromPreorderTraversal = new BSTFromPreorderTraversalC(); NodeInt root = bstFromPreorderTraversal.ConstructBSTFromPreorderTraversal(preOrderTraversal, preOrderTraversal.Length); TreeTraversals treeTraversals = new TreeTraversals(); treeTraversals.PreOrderTreeTraversal(root); }
public int FindMaxPathFromRoot(NodeInt node) { if (node == null) { return(0); } int leftNodeSum = FindMaxPathFromRoot(node.left); int rightNodeSum = FindMaxPathFromRoot(node.right); return(node.Value + Math.Max(leftNodeSum, rightNodeSum)); }
public void PostOrderTreeTraversal(NodeInt node) { if (node == null) { return; } PostOrderTreeTraversal(node.left); PostOrderTreeTraversal(node.right); Console.Write(node.Value + " "); }
public NodeInt ConstructBSTFromPreorderTraversal(int[] preorderTraversal, int size) { // The first element of pre[] is always root NodeInt root = new NodeInt(preorderTraversal[0]); Stack <NodeInt> s = new Stack <NodeInt>(); // Push root s.Push(root); // Iterate through rest of the size-1 items of given preorder array for (int i = 1; i < size; ++i) { NodeInt temp = null; /* Keep on popping while the next value is greater than * stack's top value. */ while (s.Count > 0 && preorderTraversal[i] > s.Peek().Value) { temp = s.Pop(); } // Make this greater value as the right child // and push it to the stack if (temp != null) { temp.right = new NodeInt(preorderTraversal[i]); s.Push(temp.right); } // If the next value is less than the stack's top // value, make this value as the left child of the // stack's top node. Push the new node to stack else { temp = s.Peek(); temp.left = new NodeInt(preorderTraversal[i]); s.Push(temp.left); } } return(root); }
public void RootToLeafSumM() { NodeInt root = new NodeInt(10) { left = new NodeInt(20), right = new NodeInt(30) }; root.left.left = new NodeInt(100) { left = new NodeInt(50), right = new NodeInt(100) }; root.left.right = new NodeInt(200) { left = new NodeInt(40), right = new NodeInt(20) }; RootToLeafSum rootToLeafSum = new RootToLeafSum(); Console.WriteLine(rootToLeafSum.FindRootToLeafSum(root, 270)); }
public void MaxSumPathFromRootM() { NodeInt root = new NodeInt(10) { left = new NodeInt(20), right = new NodeInt(30) }; root.left.left = new NodeInt(10) { left = new NodeInt(50), right = new NodeInt(100) }; root.left.right = new NodeInt(200) { left = new NodeInt(40), right = new NodeInt(20) }; MaxPathFromRoot maxPathFromRoot = new MaxPathFromRoot(); maxPathFromRoot.FindMaxPathFromRoot(root); }
public bool FindRootToLeafSum(NodeInt node, int sum) { // if (node == null) // { // return (sum == 0); // } int mySum = sum - node.Value; bool result = false; if (mySum == 0 && node.left == null && node.right == null) { return(true); } if (mySum > 0 && node.left != null) { result = result || FindRootToLeafSum(node.left, mySum); } if (mySum > 0 && node.right != null) { result = result || FindRootToLeafSum(node.right, mySum); } return(result); }