二叉树的最大深度

二叉树的最大深度为根节点到最远叶子节点的最长路径上的节点数。

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

示例

let root = {
    left:{
        left:{

        },
        right:{
            left:{

            },
            right:{
                left:{

                },
                right:{
                    
                }
            }
        }
    },
    right:{
        left:{
            left:{

            },
            right:{
                
            }
        },
        right:{
            left:{
                left:{
                    left:{
                        left:null,
                        right:{
                            
                        }
                    },
                    right:{
                        
                    }
                },
                right:{
                    
                }
            },
            right:{
                
            }
        }
    }
}
maxDepth(root) //7
以自己现在的努力程度,还没有资格和别人拼天赋
原文地址:https://www.cnblogs.com/zhenjianyu/p/13565150.html