美文网首页
111.二叉树的最小深度

111.二叉树的最小深度

作者: youzhihua | 来源:发表于2019-11-14 17:10 被阅读0次

题目描述

给定一个二叉树,找出其最小深度。

最小深度是从根节点到最近叶子节点的最短路径上的节点数量。

说明: 叶子节点是指没有子节点的节点。

示例:

给定二叉树 [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7
返回它的最小深度  2.

迭代思路

1.层级遍历二叉树,当遍历到叶子结点时,结束循环即可。

Java代码实现

    public int minDepth(TreeNode root) {
        int count = 0;
        LinkedList<TreeNode> treeNodes = new LinkedList<>();
        if(root != null)
            treeNodes.add(root);
        else
            return 0;
        int curMax = 1;
        int nodeCount=0;
        //层级遍历
        while(!treeNodes.isEmpty()){
            TreeNode cur = treeNodes.pollFirst();
            nodeCount++;
            if(cur.left == null && cur.right == null)
                break;
            if (cur.left != null)
                treeNodes.add(cur.left);
            if(cur.right != null)
                treeNodes.add(cur.right);
            if(nodeCount == curMax){
                //深度+1
                count++;
                nodeCount=0;
                curMax = treeNodes.size();
            }
        }
        return count+1;
    }

Golang代码实现

func minDepth(root *TreeNode) int {
    if root == nil{
        return 0
    }
    nodeList := list.New()
    nodeList.PushBack(root)
    depth := 1
    curMax := 1
    count := 0
    for nodeList.Len() != 0 {
        count++
        curNode := nodeList.Front()
        if curNode.Value.(*TreeNode).Left == nil && curNode.Value.(*TreeNode).Right == nil{
            break
        }
        nodeList.Remove(curNode)
        if curNode.Value.(*TreeNode).Left != nil{
            nodeList.PushBack(curNode.Value.(*TreeNode).Left)
        }

        if curNode.Value.(*TreeNode).Right != nil{
            nodeList.PushBack(curNode.Value.(*TreeNode).Right)
        }
        if curMax == count{
            curMax = nodeList.Len()
            depth++
            count = 0
        }
    }
    return depth
}

递归思路

  1. 递归处理没一个结点,在处理时保存并处理当前深度
  2. 若当前结点是叶子结点,结束递归即可。

Java代码实现

    public int minDepth(TreeNode root) {
        if(root == null)
            return 0;
        return minDepth(root,0);
    }
    
    private int minDepth(TreeNode root,int depth){
        if(root.left == null && root.right == null){
            return depth+1;
        }
        
        if(root.left != null && root.right != null){
            return Math.min(minDepth(root.left,depth+1),minDepth(root.right,depth+1));
        }else if(root.left != null && root.right == null){
            return minDepth(root.left,depth+1);
        }else{
            return minDepth(root.right,depth+1);
        }
    }

Golang代码实现

func minDepth(root *TreeNode) int {
    if root == nil{
        return 0
    }
    return minDepthDG(root,0);
}

func minDepthDG(root *TreeNode,depth int) int {
    if root.Left == nil && root.Right == nil{
        return depth+1
    }else{
        if root.Left != nil && root.Right != nil{
            leftDepth := minDepthDG(root.Left,depth+1)
            rightDepth := minDepthDG(root.Right,depth+1)
            
            if leftDepth<rightDepth{
                return leftDepth
            }else {
                return rightDepth
            }
        }else if root.Left == nil && root.Right != nil{
            return minDepthDG(root.Right,depth+1)
        }else {
            return minDepthDG(root.Left,depth+1)
        }
    }
}

相关文章

  • 111.二叉树的最小深度

    题目#111.二叉树的最小深度 给定一个二叉树,找出其最小深度。最小深度是从根节点到最近叶子节点的最短路径上的节点...

  • LeetCode 111. 二叉树的最小深度(Minimum D

    111. 二叉树的最小深度 给定一个二叉树,找出其最小深度。 最小深度是从根节点到最近叶子节点的最短路径上的节点数...

  • [LeetCode]111. 二叉树的最小深度

    111. 二叉树的最小深度给定一个二叉树,找出其最小深度。最小深度是从根节点到最近叶子节点的最短路径上的节点数量。...

  • 111. 二叉树的最小深度

    111. 二叉树的最小深度 给定一个二叉树,找出其最小深度。最小深度是从根节点到最近叶子节点的最短路径上的节点数量...

  • LeetCode 111-115

    111. 二叉树的最小深度[https://leetcode-cn.com/problems/minimum-de...

  • python实现leetcode之111. 二叉树的最小深度

    解题思路 广度优先遍历在节点没有孩子节点的时候就是末梢,这时深度最小的叶子结点,直接返回 111. 二叉树的最小深...

  • BFS 算法解题套路框架

    读完本文,你可以去力扣拿下如下题目: 111.二叉树的最小深度[https://leetcode-cn.com/p...

  • 111. Minimum Depth of Binary Tre

    题目 给定一个二叉树,求二叉树最小深度 解析 一个二叉树的最小深度,就是求左子树最小深度或者右子树最小深度,然后加...

  • 每日Leetcode—算法(11)

    110.平衡二叉树 算法: 111.二叉树的最小树深 算法: 112.路径总和 算法:

  • 二叉树面试题基本问题

    二叉树的最大深度与最小深度 二叉树的最大深度 最大深度是指二叉树根节点到该树叶子节点的最大路径长度。而最小深度自然...

网友评论

      本文标题:111.二叉树的最小深度

      本文链接:https://www.haomeiwen.com/subject/dborictx.html