• 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.

    此题需要注意的是元素一侧没有子树的情况。比如下个case,没有右子树,所以是左边子树的最短路径+root的这个1。

    public int MinDepth(TreeNode root) {
            if(root == null) return 0;
            if(root.left == null) return MinDepth(root.right)+1;
            if(root.right == null) return MinDepth(root.left)+1;
            return Math.Min(MinDepth(root.left), MinDepth(root.right))+1;
        }
  • 相关阅读:
    然乌湖
    邦达 八宿
    芒康
    巴塘
    禾尼乡 所波大叔
    世界高城 理塘
    相克宗 藏民家
    骑行川藏--新都桥&塔公草原
    d 3
    D2
  • 原文地址:https://www.cnblogs.com/renyualbert/p/5863022.html
Copyright © 2020-2023  润新知