minimum-depth-of-binary-tree(Leetcode)

題目描述

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.

題意是尋找二叉樹的最小高度,遞歸解決思路如下。
class Solution {
public:
    int run(TreeNode *root) {
        if(root == NULL)
            return 0; 
        int lmin = run(root->left); 
        int rmin = run(root->right); 

        if(lmin==0 && rmin == 0) 
            return 1; 
        if(lmin == 0) 
            lmin = INT_MAX; 
        if(rmin == 0) 
            rmin = INT_MAX; 

        return min(lmin, rmin)+1;
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章