get DOM tree height
2022年5月1日小于 1 分钟
get DOM tree height
Question
Height of a tree is the maximum depth from root node. Empty root node have a height of 0.
If given DOM tree, can you create a function to get the height of it?
For the DOM tree below, we have a height of 4.
<div>
<div>
<p>
<button>Hello</button>
</p>
</div>
<p>
<span>World!</span>
</p>
</div>
Can you solve this both recursively and iteratively?
Code
/**
* @param {HTMLElement | null} tree
* @return {number}
*/
function getHeight(tree) {
// your code here
}