//使用二叉树层序遍历即可
/* function TreeNode(x) {
this.val = x;
this.left = null;
this.right = null;
} */
function TreeDepth(pRoot)
{
// write code here
if(!pRoot) return 0;
let queue = [];
queue.unshift(pRoot)
let res = 0;
while(queue.length > 0){
let len = queue.length;
for(let i = 0;i < len;i++){
let tmp = queue.pop();
if(tmp){
if(tmp.left) queue.unshift(tmp.left);
if(tmp.right) queue.unshift(tmp.right);
}
}
res++
}
return res;
}
module.exports = {
TreeDepth : TreeDepth
};


京公网安备 11010502036488号