- Notifications
You must be signed in to change notification settings - Fork24
Open
Labels
Description
递归 dfs
- root 为空时,高度为 0
- root 的左右子树都为空时,高度为 1
- 如果左子树或者右子树为空时,返回另一棵子树的高度
- 否则返回两棵子树的高度最小值
constminDepth=function(root){if(root===null)return0if(root.left===null&&root.right===null){return1}if(root.left===null){return1+minDepth(root.right)}if(root.right===null){return1+minDepth(root.left)}returnMath.min(minDepth(root.left),minDepth(root.right))+1}
- 时间复杂度:O(n)
- 空间复杂度:O(logn)