[leetcode][tree][dfs] Minimum Depth of Binary Tree
2015-05-21 13:28
387 查看
题目;
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.
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.
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int minDepth(TreeNode* root) { if (NULL == root) return 0; if (NULL == root->left) return 1 + minDepth(root->right);//!!!注意:要求的是根节点到叶节点(左右子树都为空)的距离 if (NULL == root->right) return 1 + minDepth(root->left); return 1 + min(minDepth(root->left), minDepth(root->right)); } };
相关文章推荐
- leetcode 111 Minimum Depth of Binary Tree(DFS)
- LeetCode 111 Minimum Depth of Binary Tree(二叉树的最短深度)(BT、DFS)(*)
- leetcode dfs Minimum Depth of Binary Tree
- [LeetCode] Minimum Depth of Binary Tree - DFS
- LeetCode 111 Minimum Depth of Binary Tree(DFS)
- LeetCode 111 Minimum Depth of Binary Tree (DFS)
- LeetCode 111 Minimum Depth of Binary Tree
- [Leetcode] Maximum |Minimum Depth of Binary Tree
- LeetCode: Minimum Depth of Binary Tree 题解
- Minimum Depth of Binary Tree -- 二叉树的最小深度 DFS 加剪枝
- LeetCode Minimum Depth of Binary Tree
- leetcode: Minimum Depth of Binary Tree
- [leetcode 111] Minimum Depth of Binary Tree
- LeetCode - Minimum Depth of Binary Tree
- LeetCode 之 Minimum Depth of Binary Tree
- Leetcode:minimum_depth_of_binary_tree题解
- leetcode || 111、Minimum Depth of Binary Tree
- LEETCODE: Minimum Depth of Binary Tree
- LeetCode:Minimum Depth of Binary Tree
- [LeetCode-111] Minimum Depth of Binary Tree (二叉树最小深度)