/**
* 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 dep;
void dfs(TreeNode*root,int d){
if(d >= dep || root == NULL) return ;
if(root->left == NULL && root->right == NULL){
dep = d;
return ;
}
dfs(root->left,d+1);
dfs(root->right,d+1);
}
int minDepth(TreeNode* root) {
dep = 1000000000;
if(root == NULL) return 0;
dfs(root,1);
return dep;
}
};leetcode 111. Minimum Depth of Binary Tree
最新推荐文章于 2025-12-10 14:25:37 发布
本文介绍了一种求解二叉树最小深度的算法实现,通过深度优先搜索(DFS)遍历二叉树来确定从根节点到最近叶子节点的最短路径。此算法对于理解和实现树形数据结构的基本操作非常有用。
329

被折叠的 条评论
为什么被折叠?



