Minimum Depth of Binary Tree(Java)

题目描述:

 * 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.
 * 求根节点到叶子节点的最短路径的节点个数。

思路:

 * 1、递归解法,求左右子树深度,两者中小的加1即为最终深度。但是,需要注意左节点/右节点为空时,需要返回根节点到右节点/左节点的深度。
 * 2、非递归解法,按层遍历,叠加level

代码:

package com.my.test.leetcode.tree;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 题目:
 * 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.
 * 求根节点到叶子节点的最短路径的节点个数。
 * 
 * 思路:
 * 1、递归解法,求左右子树深度,两者中小的加1即为最终深度。但是,需要注意左节点/右节点为空时,需要返回根节点到右节点/左节点的深度。
 * 2、非递归解法,按层遍历,叠加level 
 */
public class MinDepthTree
{
    static class Node {
        int val;
        Node left;
        Node right;
        Node(int val) {
            this.val = val;
        }
    }
    
    /**
     * 递归解法:
     * 1、求左右子树深度,两者中小的加1即为最终深度。
     * 2、需要注意左节点/右节点为空时,需要返回根节点到右节点/左节点的深度
     *  
     * @param root 根节点
     * @return 根节点到叶子节点的节点个数
     */
    public static int getDepth(Node root) {
        if (root == null) {
            return 0;
        }
        
        int left = getDepth(root.left);
        int right = getDepth(root.right);
        
        // 需要注意左节点为空时,需要返回根节点到右叶子节点的深度
        if (left == 0 || right == 0) {
            return 1 + left + right;
        }
        
        // 两者中小的加1即为最终深度
        return left < right ? left + 1 : right + 1;
    }
    
    /**
     * 思路:
     * 非递归
     * 1、按层遍历二叉树
     * 2、遍历到该层时,判断当前节点是否为叶子节点,是叶子节点则找到了最小深度并返回,否则添加子节点到队列中
     * 
     * @param root 根节点
     * @return 最小深度
     */
    public static int getDepthLevel(Node root) {
        if (root == null) {
            return 0;
        }
        
        Queue<Node> queue = new LinkedList<Node>();
        queue.offer(root);
        int level = 0;
        
        while (!queue.isEmpty()) {
            level++;
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                Node node = queue.poll();
                // 找到了最上面的叶子节点
                if (node.left == null && node.right == null) {
                    return level;
                }
                // 非叶子节点,往队列中添加元素
                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }
            }
        }
        
        return level;
    }
    
    public static void main(String[] args)
    {
        Node root = createBinaryTree();
        System.out.println(getDepth(root));
        System.out.println(getDepthLevel(root));
    }

    private static Node createBinaryTree() {
        Node root = new Node(0);
        Node node1 = new Node(1);
        Node node2 = new Node(2);
        
        root.left = node1;
        root.right = node2;
        
        Node node3 = new Node(3);
        node1.left = node3;
        
        Node node4 = new Node(4);
        node3.left = node4;
        
        return root;
    }
}

猜你喜欢

转载自blog.csdn.net/zangdaiyang1991/article/details/88989361