树的题目
判断是否为满二叉树
判断是否为平衡二叉树
判断是否为搜索二叉树
判断是否为完全二叉树
找最低公共祖先结点
树的结点
class Node
{
public:
Node(int v) :value(v){}
int value;
Node *left;
Node *right;
};
判断是否为满二叉树
class Info
{
public:
Info(int n,int h):nodes(n),height(h){}
int nodes;
int height;
};
Info process(Node *head)
{
if (head == NULL)
{
return Info(0, 0);
}
Info leftInfo = process(head->left);
Info rightInfo = process(head->right);
int nodes = leftInfo.nodes + rightInfo.nodes + 1;
int height = (leftInfo.height > rightInfo.height ? leftInfo.height : rightInfo.height) + 1;
return Info(nodes, height);
}
bool isFull(Node *head)
{
Info info = process(head);
int N = info.nodes;
int H = info.height;
return N == (1 << H) - 1;
}
判断是否为平衡二叉树
class Info
{
public:
Info(int h,bool is):height(h),isBalance(is){}
int height;
bool isBalance;
};
Info process(Node *head)
{
if (head == NULL)
{
return Info(0, true);
}
Info leftInfo = process(head->left);
Info rightInfo = process(head->right);
int height = max(leftInfo.height, rightInfo.height) + 1;
bool isBalance = leftInfo.isBalance&&rightInfo.isBalance && (abs(leftInfo.height - rightInfo.height) < 2);
return Info(height, isBalance);
}
bool isBalance(Node *head)
{
Info info = process(head);
return info.isBalance;
}
判断是否为搜索二叉树
class Info
{
public:
Info(bool is, int ma, int mi) :isBST(is), max(ma), min(mi) {}
bool isBST;
int max;
int min;
};
Info process(Node *x)
{
if (x == NULL)
{
return Info(false, 0, 0);
}
Info leftData = process(x->left);
Info rightData = process(x->right);
int max = x->value;
int min = x->value;
if (x->left != NULL)
{
max = x->left->value > max ? x->left->value : max;
min = x->left->value < min ? x->left->value : min;
}
if (x->right != NULL)
{
max = x->right->value > max ? x->right->value : max;
min = x->right->value < min ? x->right->value : min;
}
bool isBST = false;
if (((x->left != NULL) ? (leftData.isBST && leftData.max < x->value) : true)
&&
((x->right!=NULL)?(rightData.isBST&&rightData.min>x->value):true))
{
isBST = true;
}
return Info(isBST, min, max);
}
bool isBSTTest(Node *head)
{
Info info = process(head);
return info.isBST;
}
判断是否为完全二叉树
bool isCBT(Node *head)
{
if (head == NULL)
{
return true;
}
queue<Node*>queue;
queue.push(head);
bool leaf = false;
Node *L = NULL;
Node *R = NULL;
while (!queue.empty())
{
head = queue.front();
queue.pop();
L = head->left;
R = head->right;
if (leaf && (L != NULL && R != NULL) || (L == NULL && R != NULL))
{
return false;
}
if (L != NULL)
{
queue.push(L);
}
if (R != NULL)
{
queue.push(R);
}
if (L == NULL && R == NULL)
{
leaf = true;
}
}
return true;
}
找最低公共祖先结点
class Info
{
public:
Info(bool f1,bool f2,Node* f):findO1(f1),findO2(f2),findAns(f){}
bool findO1;
bool findO2;
Node *findAns;
};
Info process(Node *x, Node *o1, Node *o2)
{
if (x == NULL)
{
return Info(false, false, NULL);
}
Info leftInfo = process(x->left, o1, o2);
Info rightInfo = process(x->right, o1, o2);
if (leftInfo.findAns != NULL)
{
return Info(true, true, leftInfo.findAns);
}
if (rightInfo.findAns != NULL)
{
return Info(true, true, rightInfo.findAns);
}
if (leftInfo.findO1 && rightInfo.findO2)
{
return Info(true, true, x);
}
if (leftInfo.findO2 && rightInfo.findO1)
{
return Info(true, true, x);
}
bool findO1 = x == o1;
bool findO2 = x == o2;
if (leftInfo.findO1 || rightInfo.findO2)
{
if (findO2) {
return Info(true, true, x);
}
else {
return Info(true, false, NULL);
}
}
if (leftInfo.findO2 || rightInfo.findO1)
{
if (findO1)
{
return Info(true, true, x);
}
else
{
return Info(false, true, NULL);
}
}
return Info(findO1, findO2, NULL);
}
Node* lowestCommon(Node *head, Node *o1, Node *o2)
{
Info info = process(head, o1, o2);
return info.findAns;
}