二叉树的套路
1. 判断二叉树是否是完全二叉树
1.1 方法一:层序遍历
- 层序遍历
- 若某节点有右孩子,无左孩子,一定不是完全二叉树
- 若第一次遇到某节点,其左右节点为NULL,剩下的节点一定是叶子节点
java
public static class Node {
public int value;
public Node left;
public Node right;
public Node(int data) {
this.value = data;
}
}java
public static boolean isCBT1(Node head) {
if (head == null) {
return true;
}
LinkedList<Node> queue = new LinkedList<>();
// 是否遇到过左右两个孩子不双全的节点
boolean leaf = false;
Node l = null;
Node r = null;
queue.add(head);
while (!queue.isEmpty()) {
head = queue.poll();
l = head.left;
r = head.right;
if (
// 如果遇到了不双全的节点之后,又发现当前节点不是叶节点
(leaf && (l != null || r != null))
||
(l == null && r != null)
) {
return false;
}
if (l != null) {
queue.add(l);
}
if (r != null) {
queue.add(r);
}
// 当遇到非全孩子时,之后的每一个节点一定是叶节点
if (l == null || r == null) {
leaf = true;
}
}
return true;
}1.2 方法二:模板
- 左满 右满 左高 == 右高
- 左完 右满 左高 == 右高 +1
- 左满 右满 左高 == 右高 +1
- 左满 右完 左高 == 右高
java
public static class Node {
public int value;
public Node left;
public Node right;
public Node(int data) {
this.value = data;
}
}
public static class Info {
public boolean isFull; // 是否为满二叉树
public boolean isCBT; // 是否是完全二叉树
public int height;
public Info(boolean full, boolean cbt, int h) {
isFull = full;
isCBT = cbt;
height = h;
}
}java
public static Info process(Node x) {
if (x == null) {
return new Info(true, true, 0);
}
Info leftInfo = process(x.left);
Info rightInfo = process(x.right);
int height = Math.max(leftInfo.height, rightInfo.height) + 1;
// 当前X是否为满二叉树, 左满 右满 且 高度一样
boolean isFull = leftInfo.isFull && rightInfo.isFull && leftInfo.height == rightInfo.height;
// 是否完全二叉树
boolean isCBT = false;
if (leftInfo.isFull && rightInfo.isFull && leftInfo.height == rightInfo.height) {
isCBT = true; // 左满 右满 左高 == 右高
} else if (leftInfo.isCBT && rightInfo.isFull && leftInfo.height == rightInfo.height + 1) {
isCBT = true; // 左完 右满 左高 == 右高 +1
} else if (leftInfo.isFull && rightInfo.isFull && leftInfo.height == rightInfo.height + 1) {
isCBT = true; // 左满 右满 左高 == 右高 +1
} else if (leftInfo.isFull && rightInfo.isCBT && leftInfo.height == rightInfo.height) {
isCBT = true; // 左满 右完 左高 == 右高
}
return new Info(isFull, isCBT, height);
}java
public static boolean isCBT2(Node head) {
return process(head).isCBT;
}2. 给定一颗二叉树的头结点,判断这棵树是否是平衡二叉树
- X的左树平衡
- X的右树平衡
- |X左 - X右| < 2
java
public static class Node {
public int value;
public Node left;
public Node right;
public Node(int data) {
this.value = data;
}
}java
public static class Info{
public boolean isBalanced;
public int height;
public Info(boolean i, int h) {
isBalanced = i;
height = h;
}
}java
public static Info process(Node x) {
if(x == null) {
return new Info(true, 0);
}// 空树一定是
Info leftInfo = process(x.left);
Info rightInfo = process(x.right);
int height = Math.max(leftInfo.height, rightInfo.height) + 1;
boolean isBalanced = true;
if(!leftInfo.isBalanced) { // 左树不平
isBalanced = false;
}
if(!rightInfo.isBalanced) {
isBalanced = false;
}
if(Math.abs(leftInfo.height - rightInfo.height) > 1) {
isBalanced = false;
}
return new Info(isBalanced, height);
}java
// 主函数
public static boolean isBalanced2(Node head) {
return process(head).isBalanced;
}3. 判断是否是搜索二叉树
- X 左边是搜索二叉树
- X 右边是搜索二叉树
- X 左边的最大值
- X 右边的最小值
java
public static class Node {
public int value;
public Node left;
public Node right;
public Node(int data) {
this.value = data;
}
}java
public static class Info {
public boolean isBST;
public int max;
public int min;
public Info(boolean i, int ma, int mi) {
isBST = i;
max = ma;
min = mi;
}
}java
public static Info process(Node x) {
// 空树 上游处理
if (x == null) {
return null;
}
// 向左数 和 右树要信息
Info leftInfo = process(x.left);
Info rightInfo = process(x.right);
int max = x.value;
if (leftInfo != null) { //有左树,那么就PK下
max = Math.max(max, leftInfo.max);
}
if (rightInfo != null) {
max = Math.max(max, rightInfo.max);
}
int min = x.value;
if (leftInfo != null) {
min = Math.min(min, leftInfo.min);
}
if (rightInfo != null) {
min = Math.min(min, rightInfo.min);
}
boolean isBST = true;
if (leftInfo != null && !leftInfo.isBST) {
isBST = false; //左树不为空 且 左树不是线索二叉树,违规
}
if (rightInfo != null && !rightInfo.isBST) {
isBST = false;
}
if (leftInfo != null && leftInfo.max >= x.value) {
isBST = false; // 左树不为空 且 左树的最大值大于当前值,违规
}
if (rightInfo != null && rightInfo.min <= x.value) {
isBST = false;
}
return new Info(isBST, max, min);
}java
// 主函数
public static boolean isBST2(Node head) {
if (head == null) { // 上游处理
return true;
}
return process(head).isBST;
}4. 任何两个节点存在距离,求整颗二叉树的最大距离
- X 左树上有最大距离,与X无关
- X 右树上有最大距离,与X无关
- 经过X,左树离X最远+右树离X最远+1 (左树高度+右树高度+1)
要求:最大dis 树高
java
public static class Node {
public int value;
public Node left;
public Node right;
public Node(int data) {
this.value = data;
}
}java
public static class Info {
public int maxDistance;
public int height;
public Info(int m, int h) {
maxDistance = m;
height = h;
}
}java
public static Info process(Node x) {
if (x == null) {
return new Info(0, 0);
}
Info leftInfo = process(x.left);
Info rightInfo = process(x.right);
int height = Math.max(leftInfo.height, rightInfo.height) + 1;
int p1 = leftInfo.maxDistance;
int p2 = rightInfo.maxDistance;
int p3 = leftInfo.height + rightInfo.height + 1;
int maxDistance = Math.max(Math.max(p1, p2), p3);
return new Info(maxDistance, height);
}java
public static int maxDistance2(Node head) {
return process(head).maxDistance;
}5. 判断一棵树是否是满二叉树
- 左树是否满二叉树
- 右树是否满二叉树
- 左树和右树高度是否一样
java
public static class Node {
public int value;
public Node left;
public Node right;
public Node(int data) {
this.value = data;
}
}java
public static class Info2 {
public boolean isFull;
public int height;
public Info2(boolean f, int h) {
isFull = f;
height = h;
}
}java
public static Info2 process2(Node h) {
if (h == null) {
return new Info2(true, 0);
}
Info2 leftInfo = process2(h.left);
Info2 rightInfo = process2(h.right);
boolean isFull = leftInfo.isFull && rightInfo.isFull && leftInfo.height == rightInfo.height;
int height = Math.max(leftInfo.height, rightInfo.height) + 1;
return new Info2(isFull, height);
}java
public static boolean isFull2(Node head) {
if (head == null) {
return true;
}
return process2(head).isFull;
}6. 整颗二叉树不是线索二叉树,求子树是搜索二叉树,且节点最多有多少
子树:包括当前节点,底下全要
- X不做头,左边有MaxSize
- X不做头,右边有MaxSize
- X做头,左树和右树是搜索二叉树,且左树最大值<X,右树最小值>X,最后左边+右边+1
java
public static class Node {
public int value;
public Node left;
public Node right;
public Node(int data) {
this.value = data;
}
}java
public static class Info {
public int maxBSTSubtreeSize; //最大子树的Size
public int allSize; //整棵树的size
public int max;
public int min;
public Info(int m, int a, int ma, int mi) {
maxBSTSubtreeSize = m;
allSize = a;
max = ma;
min = mi;
}
}java
public static Info process(Node x) {
if (x == null) {
return null;
}
Info leftInfo = process(x.left);
Info rightInfo = process(x.right);
int max = x.value;
int min = x.value;
int allSize = 1;
if (leftInfo != null) {
max = Math.max(leftInfo.max, max);
min = Math.min(leftInfo.min, min);
allSize += leftInfo.allSize;
}
if (rightInfo != null) {
max = Math.max(rightInfo.max, max);
min = Math.min(rightInfo.min, min);
allSize += rightInfo.allSize;
}
int p1 = -1; //如果左树为空,那么-1
if (leftInfo != null) {
p1 = leftInfo.maxBSTSubtreeSize;
}
int p2 = -1;
if (rightInfo != null) {
p2 = rightInfo.maxBSTSubtreeSize;
}
int p3 = -1;
// 判断左树是不是搜索二叉树
boolean leftBST = leftInfo == null ? true : (leftInfo.maxBSTSubtreeSize == leftInfo.allSize);
boolean rightBST = rightInfo == null ? true : (rightInfo.maxBSTSubtreeSize == rightInfo.allSize);
if (leftBST && rightBST) {
// 判断当前节点是否大于左树的最大值
boolean leftMaxLessX = leftInfo == null ? true : (leftInfo.max < x.value);
boolean rightMinMoreX = rightInfo == null ? true : (x.value < rightInfo.min);
if (leftMaxLessX && rightMinMoreX) {
int leftSize = leftInfo == null ? 0 : leftInfo.allSize;
int rightSize = rightInfo == null ? 0 : rightInfo.allSize;
p3 = leftSize + rightSize + 1;
}
}
return new Info(Math.max(p1, Math.max(p2, p3)), allSize, max, min);
}java
public static int maxSubBSTSize2(Node head) {
if(head == null) {
return 0;
}
return process(head).maxBSTSubtreeSize;
}7. 返回二叉树中最大的二叉搜索子树的头节点
java
public static class Info {
public Node maxSubBSTHead;
public int maxSubBSTSize;
public int min;
public int max;
public Info(Node h, int size, int mi, int ma) {
maxSubBSTHead = h;
maxSubBSTSize = size;
min = mi;
max = ma;
}
}java
public static Info process(Node X) {
if (X == null) {
return null;
}
Info leftInfo = process(X.left);
Info rightInfo = process(X.right);
int min = X.value;
int max = X.value;
Node maxSubBSTHead = null;
int maxSubBSTSize = 0;
if (leftInfo != null) {
min = Math.min(min, leftInfo.min);
max = Math.max(max, leftInfo.max);
maxSubBSTHead = leftInfo.maxSubBSTHead;
maxSubBSTSize = leftInfo.maxSubBSTSize;
}
if (rightInfo != null) {
min = Math.min(min, rightInfo.min);
max = Math.max(max, rightInfo.max);
if (rightInfo.maxSubBSTSize > maxSubBSTSize) {
maxSubBSTHead = rightInfo.maxSubBSTHead;
maxSubBSTSize = rightInfo.maxSubBSTSize;
}
}
if ((leftInfo == null ? true : (leftInfo.maxSubBSTHead == X.left && leftInfo.max < X.value))
&& (rightInfo == null ? true : (rightInfo.maxSubBSTHead == X.right && rightInfo.min > X.value))) {
maxSubBSTHead = X;
maxSubBSTSize = (leftInfo == null ? 0 : leftInfo.maxSubBSTSize)
+ (rightInfo == null ? 0 : rightInfo.maxSubBSTSize) + 1;
}
return new Info(maxSubBSTHead, maxSubBSTSize, min, max);
}java
public static Node maxSubBSTHead2(Node head) {
if (head == null) {
return null;
}
return process(head).maxSubBSTHead;
}8. 返回A和B的最近公共祖先
8.1 方法1:模板
- X无关,左树上有答案,右树上有答案,不全
- X有关,
java
public static class Info {
public boolean findA;
public boolean findB;
public Node ans; //最终结果
public Info(boolean fA, boolean fB, Node an) {
findA = fA;
findB = fB;
ans = an;
}
}java
public static Info process(Node x, Node a, Node b) {
if (x == null) {
return new Info(false, false, null);
}
Info leftInfo = process(x.left, a, b);
Info rightInfo = process(x.right, a, b);
boolean findA = (x == a) || leftInfo.findA || rightInfo.findA;
boolean findB = (x == b) || leftInfo.findB || rightInfo.findB;
Node ans = null;
if (leftInfo.ans != null) {
ans = leftInfo.ans; // 左树已经找到了
} else if (rightInfo.ans != null) {
ans = rightInfo.ans; // 右树已经找到了
} else {
if (findA && findB) {
ans = x; // 左树和右树同时找到,当前节点为结果
}
}
return new Info(findA, findB, ans);
}8.2 方法二:Leetcode上
9. 公司问题
一颗多叉树,可以选若干节点参加聚会
直接上下级不能同时到场
问:如何让Happy值最大
java
public static class Employee {
public int happy;
public List<Employee> nexts;
public Employee(int h) {
happy = h;
nexts = new ArrayList<>();
}
}- X 来的情况下,X.happy + X.children不来的max
- X 不来的情况下,max
java
public static class Info {
public int no; // 整颗子树不来的最大收益
public int yes;
public Info(int n, int y) {
no = n;
yes = y;
}
}java
public static Info process(Employee x) {
if (x == null) {
return new Info(0, 0);
}
int no = 0;
int yes = x.happy; // 来的话就是当前x的快乐值
for (Employee next : x.nexts) {
Info nextInfo = process(next);
// 如果X不来,子代可来可不来
no += Math.max(nextInfo.no, nextInfo.yes);
// 如果X来,子代一定不来
yes += nextInfo.no;
}
return new Info(no, yes);
}java
public static int maxHappy2(Employee head) {
Info allInfo = process(head);
// 要么最大老板不来 或 来 的收益
return Math.max(allInfo.no, allInfo.yes);
}
阿源的知识库