[leetcode] 111.Mininum Depth of Binary Tree (Easy)

it2025-11-09  5

原题

寻找二叉树最短深度 这里用了dfs,beat 100%,4ms

class Solution { public: int minDepth(TreeNode *root, int minNum = 0) { if (root == NULL) { return minNum == 0 ? minNum : 999999; } if (root->left == NULL && root->right == NULL) return minNum + 1; return min(minDepth(root->left, minNum + 1), minDepth(root->right, minNum + 1)); } };

转载于:https://www.cnblogs.com/ruoh3kou/p/9893413.html

最新回复(0)