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

原题

寻找二叉树最短深度

思路:
这里用了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));
  }
};

你可能感兴趣的:([LeetCode] 111. Mininum Depth of Binary Tree (Easy))