zoukankan      html  css  js  c++  java
  • leetcode[111]Minimum Depth of Binary Tree

    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.

    /**
     * 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&&root->right==NULL)return 1+minDepth(root->left);
            if(root->left==NULL&&root->right!=NULL)return 1+minDepth(root->right);
            int leftd=1+minDepth(root->left);
            int rightd=1+minDepth(root->right);
            return leftd<rightd?leftd:rightd;
        }
    };
  • 相关阅读:
    14-定时器
    13-JS中的面向对象
    12-关于DOM操作的相关案例
    11-DOM介绍
    10-关于DOM的事件操作
    09-伪数组 arguments
    08-函数
    07-常用内置对象
    06-流程控制
    05-数据类型转换
  • 原文地址:https://www.cnblogs.com/Vae1990Silence/p/4281314.html
Copyright © 2011-2022 走看看