二叉树深度

递归法

解法一

function TreeDepth(pRoot)
{
    return TreeWalk(pRoot,0)
 }

function TreeWalk(root,deep){
    if(root){
        return  Math.max(TreeWalk(root.left,deep+1),TreeWalk(root.right,deep+1))
    }
    else return deep
}

解法二

function TreeDepth(pRoot)
{
    if(!pRoot)return 0
    return  Math.max(TreeDepth(pRoot.left)+1,TreeDepth(pRoot.right)+1)
}

非递归解法

function TreeDepth(root){
    if(root == null)return 0;
    let level = [root]
    let result = 0;
    while(level.length){
        result++
        let newLevel = []
        for(let i = 0; i < level.length;i++){
            if(level[i].left)newLevel.push(level[i].left);
            if(level[i].right)newLevel.push(level[i].right);
        }
        level = newLevel;
    }
    return result;
}