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、当根为空时,输出0。
* 2、当左子树为空时,输出右子树深度+1。
* 3、当右子树为空时,输出左子树深度+1。
* 4、以上条件都不满足时,输出min(左子树深度,右子树深度)+1。
为什么加1呢?
因为根节点的深度是1。
所以要遍历二叉树,已知节点的设置是
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
程序如下:
public static int MinTree(Node node){
if (node == null)
return 0;
int lef = MinTree(node.leftChild);
System.out.print(node.data + ": 左孩子 " + lef + " * ");
int rig = MinTree(node.rightChild);
System.out.print(node.data + ": 右孩子" + rig + " - ");
System.out.println();
if (node.leftChild == null)
return rig + 1;
if (node.rightChild == null)
return lef + 1;
return Math.min(lef, rig) + 1;
}
如果树是 int[] tree = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
结果是
8: 左孩子 0 * 8: 右孩子0 -
4: 左孩子 1 * 9: 左孩子 0 * 9: 右孩子0 -
4: 右孩子1 -
2: 左孩子 2 * 10: 左孩子 0 * 10: 右孩子0 -
5: 左孩子 1 * 5: 右孩子0 -
2: 右孩子2 -
1: 左孩子 3 * 6: 左孩子 0 * 6: 右孩子0 -
3: 左孩子 1 * 7: 左孩子 0 * 7: 右孩子0 -
3: 右孩子1 -
1: 右孩子2 -
最小深度 3
可以看出是按照中序遍历
******************************************************************
补充一点,如果是最大深度
输出二叉树最大深度
* 核心思想:根节点到达最远的叶子节点的路径长度。
* 1、如果二叉树为空,则深度为0;
* 2、如果不为空,分别求左子树的深度和右子树的深度,取较大值加1,因为根节点深度是1。
*******************************************************************
再补充一点,链表生成二叉树的程序如下:
package com.zhao.tree;
import java.util.LinkedList;
import java.util.List;
import org.junit.Test;
public class BinTree {
private int[] tree = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
private static List<Node> nodeList = null;
private static class Node {
Node leftChild;
Node rightChild;
int data;
Node(int newdata) {
leftChild = null;
rightChild = null;
data = newdata;
}
}
public void CreatTree() {
nodeList = new LinkedList<Node>();
// 建立二叉树的链表
for (int nodeIndex = 0; nodeIndex < tree.length; nodeIndex++) {
nodeList.add(new Node(tree[nodeIndex]));
}
// 赋值
for (int nodeIndex = 0; nodeIndex < tree.length / 2 - 1; nodeIndex++) {
nodeList.get(nodeIndex).leftChild = nodeList.get(nodeIndex * 2 + 1);
nodeList.get(nodeIndex).rightChild = nodeList.get(nodeIndex * 2 + 2);
}
int lastindex = tree.length / 2 - 1;
nodeList.get(lastindex).leftChild = nodeList.get(lastindex * 2 + 1);
if (lastindex % 2 == 1)
nodeList.get(lastindex).rightChild = nodeList.get(lastindex * 2 + 2);
}
/*
* 遍历
*/
public static void proTree(Node node) {
// 前序
if (node == null)
return;
System.out.print(node.data + "-");
proTree(node.leftChild);
proTree(node.rightChild);
}
public static void inTree(Node node) {
// 中序
if (node == null)
return;
inTree(node.leftChild);
System.out.print(node.data + "-");
inTree(node.rightChild);
}
public static void postTree(Node node) {
// 后序
if (node == null)
return;
postTree(node.leftChild);
postTree(node.rightChild);
System.out.print(node.data + "-");
}
public static void main(String[] args) {
BinTree binTree = new BinTree();
binTree.CreatTree();
Node root = nodeList.get(0);
System.out.println("前序遍历: ");
proTree(root);
System.out.println();
System.out.println("中序遍历");
inTree(root);
System.out.println();
System.out.println("后序遍历");
postTree(root);
System.out.println();
}
}