zoukankan      html  css  js  c++  java
  • [LeetCode] 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.

    解法:参照http://blog.csdn.net/sbitswc/article/details/26526031

     1 /**
     2  * Definition for binary tree
     3  * struct TreeNode {
     4  *     int val;
     5  *     TreeNode *left;
     6  *     TreeNode *right;
     7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     8  * };
     9  */
    10 class Solution {
    11 public:
    12     int minDepth(TreeNode *root) {
    13         if (root == NULL) return 0;
    14         
    15         int left_depth = minDepth(root->left);
    16         int right_depth = minDepth(root->right);
    17     
    18         if (left_depth == 0 && right_depth == 0)
    19             return 1;
    20         if (left_depth == 0)
    21             left_depth = INT_MAX;
    22         if (right_depth == 0)
    23             right_depth = INT_MAX;
    24             
    25         return min(left_depth, right_depth) + 1;
    26     }
    27 };
  • 相关阅读:
    数据结构
    java web
    C++
    SQL(结构化查询语言)
    网站协议
    python
    爬虫
    select 多选
    List 去除重复数据的五种方式
    oracle锁表SID查询
  • 原文地址:https://www.cnblogs.com/vincently/p/4302095.html
Copyright © 2011-2022 走看看