111.二叉树的最小深度

在这里插入图片描述

/**
 * 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 (!root) return 0;
  if (!root->left&&root->right) return minDepth(root->right) + 1;
  if (root->left && !root->right) return minDepth(root->left) + 1;
  return MIN(minDepth(root->left), minDepth(root->right)) + 1;
 }
 int MIN(int a, int b)
 {
  return a > b ? b : a;
 }
};
发布了90 篇原创文章 · 获赞 7 · 访问量 2167

猜你喜欢

转载自blog.csdn.net/weixin_43784305/article/details/103098915