LeetCode-Minimum Depth of Binary Tree

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int minDepth(TreeNode *root) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if (root == NULL)
        {
            return 0;
        }
        else if (root->left != NULL && root->right == NULL)
        {
            return 1 + minDepth(root->left);
        }
        else if (root->left == NULL && root->right != NULL)
        {
            return 1 + minDepth(root->right);
        }
        else
        {
            return 1 + min(minDepth(root->left), minDepth(root->right));
        }
    }
};

發佈了124 篇原創文章 · 獲贊 1 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章