10.11树的最大(小)深度和判断对称树
import java.util.LinkedList;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
trees1<Integer> t = new trees1<>(1);
trees1<Integer> t1 = new trees1<>(2);
trees1<Integer> t2 = new trees1<>(3);
trees1<Integer> t3 = new trees1<>(4);
trees1<Integer> t4 = new trees1<>(5);
t.left = t1;
t.right = t2;
t1.left = t3;
t1.right = t4;
System.out.println("最大深度");
System.out.println(t.maxDepth(t));
System.out.println("最小深度");
System.out.println(t.minDepth(t));
System.out.println("最小深度");
System.out.println(t.min1Depth(t));
System.out.println(t.duichen(t));
trees1<Integer> s = new trees1<>(1);
trees1<Integer> s1 = new trees1<>(2);
trees1<Integer> s2 = new trees1<>(2);
s.left = s1;
s.right = s2;
System.out.println(t.duichen(s));
}
public static class trees1<T> {
private T data;
public trees1<T> left;
public trees1<T> right;
public trees1(T data) {
this.data = data;
this.left = null;
this.right = null;
}
public trees1(T data, trees1<T> left, trees1<T> right) {
this.data = data;
this.left = left;
this.right = right;
}
public int maxDepth(trees1<T> t)//树的最大深度,后序递归
{
if (t == null) {
return 0;
}
int d1 = maxDepth(t.left);
int d2 = maxDepth(t.right);
return Integer.max(d1, d2) + 1;
}
public int min1Depth(trees1<T> t)//树的最小深度,后序递归
{
if (t == null) {
return 0;
}
int d1 = min1Depth(t.left);
int d2 = min1Depth(t.right);
if (d1 == 0) {
return d2 + 1;
}
if (d2 == 0) {
return d1 + 1;
}
return Integer.min(d1, d2) + 1;
}
public int minDepth(trees1<T> t)//树的最小深度,层序遍历
{
if (t == null) {
return 0;
}
int depth = 0;
Queue<trees1> A = new LinkedList<>();
A.add(t);
while (t != null) {
int size = A.size();
depth++;
for (int i = 0; i < size; i++) {
t = A.poll();
if (t.left == null && t.right == null) {
return depth;
}
if (t.left != null) {
A.add(t.left);
}
if (t.right != null) {
A.add(t.right);
}
}
}
return depth;
}
public boolean duichen(trees1<T> t)//对称树判断
{
return check(t.left, t.right);
}
public boolean check(trees1<T> left, trees1<T> right) {
if (left == null && right == null) {
return true;
}
if (left == null || right == null) {
return false;
}
if (left.data != right.data) {
return false;
}
return check(left.left, right.right) && check(left.right, right.left);
}
}
}
import java.util.Queue;
public class Main {
public static void main(String[] args) {
trees1<Integer> t = new trees1<>(1);
trees1<Integer> t1 = new trees1<>(2);
trees1<Integer> t2 = new trees1<>(3);
trees1<Integer> t3 = new trees1<>(4);
trees1<Integer> t4 = new trees1<>(5);
t.left = t1;
t.right = t2;
t1.left = t3;
t1.right = t4;
System.out.println("最大深度");
System.out.println(t.maxDepth(t));
System.out.println("最小深度");
System.out.println(t.minDepth(t));
System.out.println("最小深度");
System.out.println(t.min1Depth(t));
System.out.println(t.duichen(t));
trees1<Integer> s = new trees1<>(1);
trees1<Integer> s1 = new trees1<>(2);
trees1<Integer> s2 = new trees1<>(2);
s.left = s1;
s.right = s2;
System.out.println(t.duichen(s));
}
public static class trees1<T> {
private T data;
public trees1<T> left;
public trees1<T> right;
public trees1(T data) {
this.data = data;
this.left = null;
this.right = null;
}
public trees1(T data, trees1<T> left, trees1<T> right) {
this.data = data;
this.left = left;
this.right = right;
}
public int maxDepth(trees1<T> t)//树的最大深度,后序递归
{
if (t == null) {
return 0;
}
int d1 = maxDepth(t.left);
int d2 = maxDepth(t.right);
return Integer.max(d1, d2) + 1;
}
public int min1Depth(trees1<T> t)//树的最小深度,后序递归
{
if (t == null) {
return 0;
}
int d1 = min1Depth(t.left);
int d2 = min1Depth(t.right);
if (d1 == 0) {
return d2 + 1;
}
if (d2 == 0) {
return d1 + 1;
}
return Integer.min(d1, d2) + 1;
}
public int minDepth(trees1<T> t)//树的最小深度,层序遍历
{
if (t == null) {
return 0;
}
int depth = 0;
Queue<trees1> A = new LinkedList<>();
A.add(t);
while (t != null) {
int size = A.size();
depth++;
for (int i = 0; i < size; i++) {
t = A.poll();
if (t.left == null && t.right == null) {
return depth;
}
if (t.left != null) {
A.add(t.left);
}
if (t.right != null) {
A.add(t.right);
}
}
}
return depth;
}
public boolean duichen(trees1<T> t)//对称树判断
{
return check(t.left, t.right);
}
public boolean check(trees1<T> left, trees1<T> right) {
if (left == null && right == null) {
return true;
}
if (left == null || right == null) {
return false;
}
if (left.data != right.data) {
return false;
}
return check(left.left, right.right) && check(left.right, right.left);
}
}
}
本文来自博客园,作者:赵千万,转载请注明原文链接:https://www.cnblogs.com/zhaoqianwan/p/17758090.html
千万千万赵千万