数据结构与算法学习(6)
一.二叉树的补充
1.如何判断一棵树是否是搜索二叉树
Info1* Process01(TreeNode* head) {
if (head == nullptr) {
return nullptr;
}
Info1* leftInfo = Process01(head->left);
Info1* rightInfo = Process01(head->right);
int minNum = head->val;
int maxNum = head->val;
if (leftInfo != nullptr) {
minNum = min(minNum, leftInfo->min);
maxNum = max(maxNum, leftInfo->max);
}
if (rightInfo != nullptr) {
minNum = min(minNum, rightInfo->min);
maxNum = max(maxNum, rightInfo->max);
}
bool isBst = true;
if (leftInfo != nullptr && (!leftInfo->isBst || leftInfo->max >= head->val))
isBst = false;
if (rightInfo != nullptr && (!rightInfo->isBst || rightInfo->min <= head->val))
isBst = false;
delete leftInfo;
delete rightInfo;
return new Info1(isBst, minNum, maxNum);
}
2.如何判断一颗树是否是完全二叉树
bool Test02(TreeNode* head) {
queue<TreeNode*> q;
q.push(head);
bool target = false;
while (!q.empty())
{
TreeNode* node = q.front();
TreeNode* l = node->left;
TreeNode* r = node->right;
q.pop();
if ((target && (l != nullptr || r != nullptr))
||
(l == nullptr && r != nullptr))
return false;
if (l != nullptr)
q.push(l);
if (r != nullptr)
q.push(r);
if (l == nullptr || r == nullptr)
target = true;
}
return true;
}
3.如何判断一棵树是否是满二叉树
Info2* Process02(TreeNode* head) {
if (head == nullptr)
return new Info2(0,0);
Info2* leftInfo = Process02(head->left);
Info2* rightInfo = Process02(head->right);
int height = max(leftInfo->height, rightInfo->height) + 1;
int nodes = leftInfo->nodes + rightInfo->nodes + 1;
delete leftInfo;
delete rightInfo;
return new Info2(height,nodes);
}
bool Test03(TreeNode* head) {
if (head == nullptr)
return true;
Info2* info = Process02(head);
return info->nodes == (1 << info->height - 1);
}
4.如何判断一棵树是否是平衡二叉树
Info3* Process03(TreeNode* head) {
if (head == nullptr)
return new Info3(true, 0);
Info3* leftInfo = Process03(head->left);
Info3* rightInfo = Process03(head->right);
int height = max(leftInfo->height, rightInfo->height) + 1;
bool isOk = leftInfo->isOk && rightInfo->isOk && (abs(leftInfo->height - rightInfo->height) <= 1);
return new Info3(isOk, height);
}
5.二叉树的最低公共父节点
TreeNode* GetCommonFatherNode(TreeNode* head,TreeNode* node1,TreeNode* node2) {
if (head == nullptr || head == node1 || head == node2)
return head;
TreeNode* left = GetCommonFatherNode(head->left, node1, node2);
TreeNode* right = GetCommonFatherNode(head->right, node1, node2);
if (left != nullptr && right != nullptr)
return head;
return left != nullptr ? left : right;
}
6.求某一个节点的后继节点
中序遍历的下一个节点为后续节点
NewTreeNode* GetLastNode(NewTreeNode* node) {
if (node == nullptr)return nullptr;
NewTreeNode* tmp = nullptr;
if (node->right != nullptr) {
tmp = node->right;
while (tmp)
{
tmp = tmp->left;
}
return tmp;
}
else {
tmp = node;
NewTreeNode* parent = node->parent;
while (parent != nullptr && tmp == parent->right)
{
tmp = tmp->parent;
parent = parent->parent;
}
return parent;
}
}
7.字条对折打印凹凸性
void Process04(int i, int n, bool isAo) {
if (i > n)
return;
Process04(i + 1, n, true);
string s = isAo ? "凹 " : "凸 ";
cout << s;
Process04(i + 1, n, false);
}
void Test04(int n) {
Process04(1, n, true);
}