设为首页 加入收藏

TOP

leetcode - Minimum Depth of Binary Tree
2015-07-20 17:33:56 来源: 作者: 【 】 浏览:1
Tags:leetcode Minimum Depth Binary Tree

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 binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
struct TreeNode
{
	int val;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
    int minDepth(TreeNode *root) {
		min = INT_MAX;
		dfs(root,1);
		return min;
    }
private:
	int min;
	void dfs(TreeNode *root,int level)
	{
		if(root == NULL) return ;
		if(root->left == NULL && root->right == NULL)
		{
			if(level < min) min = level;
		}
		else
		{
			dfs(root->left,level+1);
			dfs(root->right,level+1);
		}
	}
};


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDU 5044 Tree(树链剖分) 下一篇hdu 4722 数位dp

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·PostgreSQL 索引 - (2025-12-25 22:20:43)
·MySQL Node.js 连接 (2025-12-25 22:20:41)
·SQL 撤销索引、表以 (2025-12-25 22:20:38)
·Linux系统简介 (2025-12-25 21:55:25)
·Linux安装MySQL过程 (2025-12-25 21:55:22)