c_cpp Binary Treehttp的最小深度://oj.leetcode.com/problems/minimum-depth-of-binary-tree/

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp Binary Treehttp的最小深度://oj.leetcode.com/problems/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) {
        if(root == NULL){
            return 0;
        }
        if(root->left == NULL && root->right == NULL){
            return 1;
        }
        if(root->left == NULL){
            return minDepth(root->right) + 1;
        }
        if(root->right == NULL){
            return minDepth(root->left) + 1;
        }
        int l = minDepth(root->left);
        int r = minDepth(root->right);
        return (l < r) ? l+1 : r+1;
    }
};

以上是关于c_cpp Binary Treehttp的最小深度://oj.leetcode.com/problems/minimum-depth-of-binary-tree/的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 验证二进制搜索Treehttp://oj.leetcode.com/problems/validate-binary-search-tree/

c_cpp binary_search

c_cpp decimal2binary

c_cpp all_binary_paths.cpp

c_cpp 67.添加Binary-DifficultyEasy - 2018.8.23

c_cpp 145. Binary Tree Postorder Traversal - DifficultyHard - 2018.9.7