JSFiddle - React, Tailwind, and code Playground
by Diana Lemen
JavaScript
const tree = {
value: 1,
left: {
value: 2,
left: {
value: 4,
left: {
value: 6,
left: null,
right: null
},
right: {
value: 7,
left: null,
right: {
value: 9,
left: {
value: 10,
left:null,
right: null
},
right: null
}
}
},
right: null
},
right: {
value: 3,
left: null,
right: {
value: 5,
left: {
value: 8,
left: null,
right: null
},
right: null
}
}
}
function goTgroughtTree(tree, curDepth, treeDepth) {
treeDepth = curDepth > treeDepth ? curDepth : treeDepth;
if(tree.right) goTgroughtTree(tree.right, curDepth + 1);
if(tree.left) goTgroughtTree(tree.left, curDepth + 1);
return treeDepth;
}
console.log(goTgroughtTree(tree, 0, 0));