111. Minimum Depth of Binary Tree
2017-04-19 23:57
134 查看
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
求二叉树最小深度,注意是叶子节点距离跟节点的最小深度。
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
求二叉树最小深度,注意是叶子节点距离跟节点的最小深度。
/** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ /** * @param {TreeNode} root * @return {number} */ var minDepth = function(root) { if(root===null)return 0; var left=minDepth(root.left); var right=minDepth(root.right); if(left===0)return right+1; if(right===0)return left+1; return (left<right)?(left+1):(right+1) };
相关文章推荐
- 226. Invert Binary Tree
- 104. Maximum Depth of Binary Tree
- Sicily 1210 二叉树 (Binary tree)
- POJ-2499 Binary Tree
- 笔试算法题(41):线索二叉树(Threaded Binary Tree)
- Binary Tree: Inorder Traversal
- Binary Tree
- Binary Tree(POJ--2499
- hdu 5573 Binary Tree(构造)
- 【LeetCode】111. Minimum Depth of Binary Tree
- 剑指offer 面试题19:二叉树的镜像(leetcode226. Invert Binary Tree) 题解
- Leetcode Everyday: 104. Maximum Depth of Binary Tree
- LeetCode 104. Maximum Depth of Binary Tree
- 104. Maximum Depth of Binary Tree
- Data Structure:Binary Tree
- LeetCode 111. Minimum Depth of Binary Tree
- Binary Tree
- LeetCode 111. Minimum Depth of Binary Tree
- 104. Maximum Depth of Binary Tree
- 543. Diameter of Binary Tree