private static Result FindPath(tree node){ //find the number of nodes on the longest path if (node == null) //handle edge case return new Result (0, 0, 0); Result leftResult = FindPath (node.l); Result rightResult = FindPath (node.r); int leftLength = leftResult.leftLength + 1; int rightLength = rightResult.rightLength + 1; int maxLength = Math.Max (Math.Max (leftLength, rightLength), Math.Max (leftResult.maxLength, rightResult.maxLength)); return new Result (leftLength, rightLength, maxLength); }
private static Result FindPath(tree node) //find the number of nodes on the longest path { if (node == null) //handle edge case { return(new Result(0, 0, 0)); } Result leftResult = FindPath(node.l); Result rightResult = FindPath(node.r); int leftLength = leftResult.leftLength + 1; int rightLength = rightResult.rightLength + 1; int maxLength = Math.Max(Math.Max(leftLength, rightLength), Math.Max(leftResult.maxLength, rightResult.maxLength)); return(new Result(leftLength, rightLength, maxLength)); }
public static void Main(string[] args) { tree node1 = new tree(5); tree node2 = new tree(2); tree node3 = new tree(15); tree node4 = new tree(10); tree node5 = new tree(6); tree node6 = new tree(14); node1.l = node2; node1.r = node3; node3.l = node4; node4.l = node5; node4.r = node6; Console.WriteLine(Finder.FindLongestPath(node1)); //2 }
public static void Main (string[] args) { tree node1 = new tree (5); tree node2 = new tree (2); tree node3 = new tree (15); tree node4 = new tree (10); tree node5 = new tree (6); tree node6 = new tree (14); node1.l = node2; node1.r = node3; node3.l = node4; node4.l = node5; node4.r = node6; Console.WriteLine (Finder.FindLongestPath (node1)); //2 }
public static int FindLongestPath(tree root){ Result result = FindPath (root); return result.maxLength-1; //length of path is the number of nodes minus 1, do not forget this! }
public tree(int x){ //for test use only this.x = x; this.l = null; this.r = null; }
public static int FindLongestPath(tree root) { Result result = FindPath(root); return(result.maxLength - 1); //length of path is the number of nodes minus 1, do not forget this! }
public tree(int x) //for test use only { this.x = x; this.l = null; this.r = null; }