class Node {
constructor(key) {
this
.key = key;
this
.left =
null
;
this
.right =
null
;
}
}
function
newNode(key) {
const temp =
new
Node(key);
return
temp;
}
function
findPath(root, name, maxV) {
if
(root ===
null
) {
return
0;
}
const left = findPath(root.left,
'l'
, maxV);
const right = findPath(root.right,
'r'
, maxV);
if
(name ===
't'
) {
return
Math.max(left, right);
}
if
(left === 0 && right === 0) {
return
1;
}
else
{
if
(left < right) {
if
(name ===
'r'
)
return
1 + right;
else
{
maxV[0] = Math.max(maxV[0], right);
return
1 + left;
}
}
else
{
if
(name ===
'l'
)
return
1 + left;
else
{
maxV[0] = Math.max(maxV[0], left);
return
1 + right;
}
}
}
return
0;
}
const root = newNode(3);
root.left = newNode(3);
root.right = newNode(3);
root.left.right = newNode(2);
root.right.left = newNode(4);
root.right.left.left = newNode(4);
const maxV = [0];
const maxVal = Math.max(
findPath(root,
't'
, maxV),
maxV[0]
);
console.log(maxVal);