1.二叉树结点间的最大距离
最大距离分析:
1.根结点不参与
1.1最大距离在根结点左树
1.2最大距离在根结点右树
2.根结点参与
从根结点左树最远的结点到右树最远的结点:由高度决定
3.最大距离=上述3种情况的最大值
class Node {
public:
int value;
Node* left;
Node* right;
Node(int data) {
this->value = data;
}
};
class Info {
public:
int maxDistance;
int height;
Info(int dis, int h) {
maxDistance = dis;
height = h;
}
};
//返回以x为头的整棵树,两个信息
Info process(Node* x) {
if (x == NULL)return Info(0, 0);
Info leftInfo = process(x->left);
Info rightInfo = process(x->right);
//info
int p1 = leftInfo.maxDistance;
int p2 = rightInfo.maxDistance;
int p3 = leftInfo.height + 1 + rightInfo.height;
int maxDistance = max(p3, max(p1, p2));
int height = max(leftInfo.height, rightInfo.height) + 1;
return Info(maxDistance, height);
}
int maxDistance(Node* head) {
return process(head).maxDistance;
}
2.派对的最大快乐值
可能性分类:每个员工在上级的影响下决定来与不来,所以分成两种情况即可。
class Info {
public:
int laiMaxHappy;
int buMaxHappy;
Info(int lai, int bu) {
laiMaxHappy = lai;
buMaxHappy = bu;
}
};
class Employee {
public:
int happy;
vector<Employee>nexts;//直接下属
};
Info process(Employee x) {
if (x.nexts.empty())return Info(x.happy, 0);//x是基层员工
int lai = x.happy;//x来的情况下,整棵树的最大收益的初始值
int bu = 0;//x不来的情况下,整棵树最大收益的初始值
//遍历直接下属,提取信息
for (Employee next : x.nexts) {
//取一名下属员工
Info info = process(next);
//如果x来,那么x的直系下属就不来
lai += info.buMaxHappy;
//如果x不来,x的直系下属可来也可不来,取最大值
bu += max(info.laiMaxHappy, info.buMaxHappy);
}
return Info(lai, bu);
}
int maxHappy(Employee boss) {
Info headInfo = process(boss);
return max(headInfo.laiMaxHappy, headInfo.buMaxHappy);
}
递归实际上就是暴力地罗列出所有可能性(可能性是我们提前想出来的),然后在所有情况中选择自己想要的,由局部组合成
整体。
3.Morris遍历
class Node {
public:
int value;
Node* left;
Node* right;
Node(int val){
this->value = value;
this->left = NULL;
this->right = NULL;
}
};
void morris(Node* head) {
if (head == NULL)return;
Node* cur = head;
Node* mostRight = NULL;
while (cur != NULL) {//流程
mostRight = cur->left;//mostRight暂时作cur的左孩子
if (mostRight != NULL) {//有左子树
//由于会手动更改右指针,所以多加一个判断条件
while (mostRight->right != NULL && mostRight != cur) {
mostRight = mostRight->right;
}
//mostRight变成了cur左子树最右的节点
if (mostRight->right == NULL) {
//第一次来到cur节点
mostRight->right = cur;
cur = cur->left;
continue;
}
else {
//第二次来到cur节点,恢复指针
mostRight->right = NULL;
}
}
cur = cur->right;
}
}
先序:
1.只经过一次的节点,直接打印
2.经过两次的节点,只有第一次才打印
void morrisPre(Node* head) {
if (head == NULL)return;
Node* cur = head;
Node* mostRight = NULL;
while (cur != NULL) {//流程
mostRight = cur->left;//mostRight暂时作cur的左孩子
if (mostRight != NULL) {//有左子树,说明会遍历两次该节点
//由于会手动更改右指针,所以多加一个判断条件
while (mostRight->right != NULL && mostRight != cur) {
mostRight = mostRight->right;
}
//mostRight变成了cur左子树最右的节点,第一次遍历时才打印
if (mostRight->right == NULL) {
//第一次来到cur节点
cout << cur->value << " ";
mostRight->right = cur;
cur = cur->left;
continue;
}
else {
//第二次来到cur节点,恢复指针
mostRight->right = NULL;
}
}
else {//没有左子树的情况,说明只会遍历一次该节点,直接打印
cout << cur->value << " ";
}
cur = cur->right;
}
}
中序:
1.只经过一次的节点,直接打印
2.经过两次的节点,只有第二次才打印
void morrisIn(Node* head) {
if (head == NULL)return;
Node* cur = head;
Node* mostRight = NULL;
while (cur != NULL) {//流程
mostRight = cur->left;//mostRight暂时作cur的左孩子
if (mostRight != NULL) {//有左子树,说明会遍历两次该节点
//由于会手动更改右指针,所以多加一个判断条件
while (mostRight->right != NULL && mostRight != cur) {
mostRight = mostRight->right;
}
//mostRight变成了cur左子树最右的节点
if (mostRight->right == NULL) {
//第一次来到cur节点
mostRight->right = cur;
cur = cur->left;
continue;
}
else {
//第二次来到cur节点,恢复指针
mostRight->right = NULL;
}
}
else {//没有左子树的情况,说明只会遍历一次该节点,直接打印
cout << cur->value << " ";
}
//第二次遍历完之后跳出循环直接打印该节点
cout << cur->value << " ";
cur = cur->right;
}
}
后序:
1.只有在第二次遍历到能被二次遍历的节点时才做事:逆序打印其左树的右边界
2.最后单独逆序打印整棵树的右边界
Node* reverseEdge(Node* from) {
Node* pre = NULL;
Node* next = NULL;
while (from != NULL) {
next = from->right;
from->right = pre;
pre = from;
from = next;
}
return pre;
}
//以x为头的树,逆序打印这棵树的右边界
void printEdge(Node* x) {
Node* tail = reverseEdge(x);
Node* cur = tail;
while (cur != NULL) {
cout << cur->value << " ";
cur = cur->right;
}
reverseEdge(tail);
}
void morriPos(Node* head) {
if (head == NULL)return;
Node* cur = head;
Node* mostRight = NULL;
while (cur != NULL) {//流程
mostRight = cur->left;//mostRight暂时作cur的左孩子
if (mostRight != NULL) {//有左子树,说明会遍历两次该节点
//由于会手动更改右指针,所以多加一个判断条件
while (mostRight->right != NULL && mostRight != cur) {
mostRight = mostRight->right;
}
//mostRight变成了cur左子树最右的节点
if (mostRight->right == NULL) {
//第一次来到cur节点
mostRight->right = cur;
cur = cur->left;
continue;
}
else {
//第二次来到cur节点,恢复指针
mostRight->right = NULL;
//逆序打印左树右边界
printEdge(cur->left);
}
}
cur = cur->right;
}
//单独逆序打印整棵树右边界
printEdge(head);
}
判断搜索二叉树:
bool isBST(Node* head) {
if (head == NULL)return true;
Node* cur = head;
Node* mostRight = NULL;
int preValue = INT_MIN;
while (cur != NULL) {//流程
mostRight = cur->left;//mostRight暂时作cur的左孩子
if (mostRight != NULL) {//有左子树,说明会遍历两次该节点
//由于会手动更改右指针,所以多加一个判断条件
while (mostRight->right != NULL && mostRight != cur) {
mostRight = mostRight->right;
}
//mostRight变成了cur左子树最右的节点
if (mostRight->right == NULL) {
//第一次来到cur节点
mostRight->right = cur;
cur = cur->left;
continue;
}
else {
//第二次来到cur节点,恢复指针
mostRight->right = NULL;
}
}
if (cur->value <= preValue)return false;
preValue = cur->value;
cur = cur->right;
}
}
最优解:
如果要做第三次信息的强整合,用递归;否则,用Morris