forked from xieqilu/Qilu-leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA100.MaxLengthDescendingPathBT.cs
108 lines (90 loc) · 3.03 KB
/
A100.MaxLengthDescendingPathBT.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
A binary tree consisting of N nodes is given. We say that a node T has a left descendant(or, conversely, aright descendant)
if the value of the attribute l(or, conversely, attributer) is a value other than NULL.
A descending path is a sequence of nodes of the tree such that each successive node is a descendant of the previous one.
The length of a descending path is the number of pointers it traverses; that is, the number of nodes it contains minus 1.
Assume that the following declarations are given:
struct tree {
int x;
struct tree * l;
struct tree * r;
};
class tree{
int x;
tree l;
tree r;
}
Write a function: int solution(struct tree * T);
that, given a binary tree, returns the maximum length of a descending path that always goes to the left or always to the right.
*/
/**
Recursive Solution:
For every Node, we want to know three values:
1,the length of the left path starting at that node,
2,the length of the right path starting at that node, and
3,the length of the longest path starting at that node or one of its descendants.
For each recursive call, we need to return the above three values to its parent call.
So we need to build a new class that contains these three values and for each recursive
call, we return an objec of the class.
Base case: for any leaf node, the above three values are all zero.
*/
using System;
using System.Collections.Generic;
namespace MaxLengthDescendingPathBT
{
class tree{
public int x{ get; set;}
public tree l{ get; set;}
public tree r{ get; set;}
public tree(int x){ //for test use only
this.x = x;
this.l = null;
this.r = null;
}
}
class Result{ //class contains return information for recursive call
public int leftLength{ get; set;}
public int rightLength{ get; set;}
public int maxLength{get;set;}
public Result(int l, int r, int m){
this.leftLength = l;
this.rightLength = r;
this.maxLength = m;
}
}
class Finder{
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 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!
}
}
class MainClass
{
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
}
}
}