当前位置:网站首页>二叉树的递归套路
二叉树的递归套路
2022-06-09 06:22:00 【AloneDrifters】
可以解决面试中绝大多数的二叉树问题尤其是树型dp问题,本质是利用递归遍历二叉树的便利性

文章目录
给定一棵二叉树的头节点head,返回这颗二叉树是不是平衡二叉树
public class IsBalanced {
public static class Node {
public int value;
public Node left;
public Node right;
public Node(int data) {
this.value = data;
}
}
public static boolean isBalanced(Node head) {
return process(head).isBalanced;
}
public static class Info{
public boolean isBalanced;
public int height;
public Info(boolean i, int h) {
isBalanced = i;
height = h;
}
}
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);
}
}
给定一棵二叉树的头节点head,任何两个节点之间都存在距离,返回整棵二叉树的最大距离
public class MaxDistance {
public static class Node {
public int value;
public Node left;
public Node right;
public Node(int data) {
this.value = data;
}
}
public static int maxDistance(Node head) {
return process(head).maxDistance;
}
public static class Info {
public int maxDistance;
public int height;
public Info(int dis, int h) {
maxDistance = dis;
height = h;
}
}
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 maxDistance = Math.max(
Math.max(leftInfo.maxDistance, rightInfo.maxDistance),
leftInfo.height + rightInfo.height + 1);
return new Info(maxDistance, height);
}
}
(*)给定一棵二叉树的头节点head,返回这颗二叉树中最大的二叉搜索子树的头节点
// 在线测试链接 : https://leetcode.com/problems/largest-bst-subtree
public class MaxSubBSTSize {
// 提交时不要提交这个类
public static class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int value) {
val = value;
}
}
// 提交如下的代码,可以直接通过
public static int largestBSTSubtree(TreeNode head) {
if (head == null) {
return 0;
}
return process(head).maxBSTSubtreeSize;
}
public static class Info {
public int maxBSTSubtreeSize;
public int allSize;
public int max;
public int min;
public Info(int m, int a, int ma, int mi) {
maxBSTSubtreeSize = m;
allSize = a;
max = ma;
min = mi;
}
}
public static Info process(TreeNode x) {
if (x == null) {
return null;
}
Info leftInfo = process(x.left);
Info rightInfo = process(x.right);
int max = x.val;
int min = x.val;
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;
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.val);
boolean rightMinMoreX = rightInfo == null ? true : (x.val < 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);
}
}
边栏推荐
- Atlas7 NAND stress test program
- Talk about bladed software
- Exponential moving weighted average
- C# 委托相关
- unity3d 更换项目字体
- Transmission medium twisted pair and optical fiber and binary
- C# List排序
- Office2016 genuine key display is not activated after a period of time
- The national industrial level Quanzhi t3/a40i core board -com-x40i helps the intelligent power system
- 全志平台BSP裁剪(1)kernel裁剪--调试工具和调试信息的裁剪
猜你喜欢

Encounter nodejs

Comparison between the most cost-effective processor and domestic processor i.mx6ul/a40i/t3

照葫芦画瓢,移植qt5.12到T507开发板

全志平台BSP裁剪(4)kernel裁剪--File systems & driver & 杂项裁剪

Xiaomi 4 failed to install wechat

Bladed software windfile calculation

全志V3s学习记录(11)音频、视频使用总结

Postman 安装

你真的懂熵了嗎(含交叉熵)

全志平台BSP裁剪(1)kernel裁剪--调试工具和调试信息的裁剪
随机推荐
自然语言之文本预处理
全志V3s学习记录(9)buildroot文件系统构建
C#字符串用法集合
iTOP-IMX6Q开发板QT5.7系统Mplayer移植-交叉编译 Libmad-0.15.1b
Bladed software windfile calculation
Bat renames files (folders) in batch
C string usage set
Transmission medium twisted pair and optical fiber and binary
Yocto compiling libdrm
Singh function sinc (x) and sampling function SA (T)
Conversion of data type real and word in PROFIBUS DP communication
Xiaomi 4 failed to install wechat
基于国产全志A40I的机器人示教器解决方案
[early spring 2022] [leetcode] 45 Jumping game II
Unity location service GPS API
[reprint] LCD common interface principle
unity iTween使用
C# 匿名函数
性价比最高处理器和国产处理器I.MX6UL/A40I/T3对比
全志平台BSP裁剪(1)kernel裁剪--调试工具和调试信息的裁剪