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

    思路:

    递归,注意是到leaf node,所以有一个孩子为空的话,则取非空的那一孩子

    package tree;
    
    public class MinimumDepthOfBinaryTree {
        
        public int minDepth(TreeNode root) {
            if (root == null) return 0;
            if (root.left == null || root.right == null) return root.left == null ? (1 + minDepth(root.right)) : (1 + minDepth(root.left));
            return Math.min(1 + minDepth(root.left), 1 + minDepth(root.right));
        }
        
    }
  • 相关阅读:
    再逛开心网
    WAPM
    win2003安装flash cs4
    [AS3][物体的运动]
    转sql产生百万记录
    KeyedList
    timer 焦点
    sql优化
    灰色
    参数
  • 原文地址:https://www.cnblogs.com/null00/p/5118201.html
Copyright © 2011-2022 走看看