一起刷leetcode(111):Minimum Depth of Binary Tree


地址:https://oj.leetcode.com/proble ... tree/
类似问题:Leetcode 104
题号:111
描述:
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.
已邀请:

beta

赞同来自: July


思路:递归
代码C++(AC):
class Solution {
public:
    int minDepth(TreeNode *root) {
        if (root == nullptr) return 0;
        int right_depth = minDepth(root->left);
        int left_depth = minDepth(root->right);
        if (right_depth == 0){
            return left_depth + 1;
        }else if (left_depth==0){
            return right_depth + 1;
        }else{
            return right_depth < left_depth ? right_depth + 1 : left_depth + 1;
        }
    }
};

要回复问题请先登录注册