5.4.1 Minimum Depth of Binary Tree

来源:互联网 发布:mac os虚拟机 编辑:程序博客网 时间:2024/06/05 03:06

Notes: 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.  Solution: 1. Recursion. Pay attention to cases when the non-leaf node has only one child. 2. Iteration + Queue. Contributed by SUN Mian(孙冕). */   /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */int BiTree::minDepth(BiNode*root)
{
if (!root)
return 0;
return 1 + min(minDepth(root->lchild), minDepth(root->rchild));


}

0 0
原创粉丝点击