记录关于二叉树的简单以及中等题
9 实训九
*9-1 建二叉树二叉链表存储
9.1.1 前序+中序
BiTree CreateBiTreeByPreIn(char *pre, char *in, int n){
if(n <= 0) return nullptr;
BiTree bt;
bt = (BiTree)malloc(sizeof(BiNode));
bt->data = pre[0];
char *p = strchr(in, pre[0]);
int len = p-in;
bt->LNode = CreateBiTreeByPreIn(pre+1, in, len);
bt->RNode = CreateBiTreeByPreIn(pre+len+1, p+1, n-len-1);
return bt;
}
9.1.2 中序+后序
BiTree CreateBiTreeByPostIn(char *post, char *in, int n){
if(n <= 0) return nullptr;
BiTree bt;
bt = (BiTree)malloc(sizeof(BiNode));
//post = post+n-1;
bt->data = post[n-1];
char *p = strchr(in, post[n-1]);
int len = p-in;
bt->LNode = CreateBiTreeByPostIn(post, in, len);
bt->RNode = CreateBiTreeByPostIn(post+len, p+1, n-len-1);
return bt;
}
9-2 求二叉树中值为x的节点所在的层号
int fun(BiTree bt, char target, int layer){
if(bt){
if(bt->val == target){
return layer;
}
int l = fun(bt->left, target, layer+1);
if(l > 0) return l;
int r = fun(bt->right, target, layer+1);
if(r > 0) return r;
}
return -1;
}
9-3 求二叉树的宽度
两种思路,一种是利用队列,一种利用栈
int fun2(BiTree bt){
if(bt){
queue<BiNode*> q;
q.push(bt);
int width = INT_MIN;
while(!q.empty()){
int len = q.size();
width = max(width, len);
for( int i = 0; i < len; i ++ ){
BiNode* tmp = q.front();
q.pop();
if(tmp->left) q.push(tmp->left);
if(tmp->right) q.push(tmp->right);
}
}
return width;
}
return 0;
}
int count[100];
int MAX = -1;
void FindWidth(BiTree bt, int k){
if(bt){
count[k] ++;
if(MAX < count[k]) MAX = count[k];
FindWidth(bt->LNode, k+1);
FindWidth(bt->RNode, k+1);
}
}
9-4 括号打印二叉树
void SpecialPrint(BiTree bt){
if(bt){
printf("%c", bt->val);
if(bt->left || bt->right){
printf("(");
if(bt->left){
SpecialPrint(bt->left);
}
if(bt->right){
printf(",");
SpecialPrint(bt->right);
}
printf(")");
}
}
}
10 实训十
10-1 判断二叉树是否为正则二叉树
正则二叉树的定义:指在二叉树中不存在度为1的分支点。
void fun11(BiTree bt, bool &isReg){
if(bt){
if((bt->left==nullptr && bt->right)||(bt->left && bt->right == nullptr)){
isReg = false;
}
fun11(bt->left, isReg);
fun11(bt->right, isReg);
}
}
bool fun1(BiTree bt){
bool isReg = true;
fun11(bt, isReg);
return isReg;
}
*10-2 判断二叉树是否为完全二叉树
//先判断leaf这一招是真的妙啊!
bool fun2(BiTree bt){
bool isCom = true;
bool isLeaf = false;
if(!bt) return isCom;
queue<BiNode*> q;
q.push(bt);
while(!q.empty()){
BiNode* tmp = q.front();
q.pop();
if(tmp->left==nullptr && tmp->right == nullptr){
isLeaf = true;
}
if(isLeaf && (tmp->left || tmp->right)){
isCom = false;
}
if(tmp->left) q.push(tmp->left);
if(tmp->right) q.push(tmp->right);
}
return isCom;
}
10-3 判断二叉树是否为二叉排序树
char pre = ' ';
bool isSorted = true;
void fun33(BiTree bt){
if(bt){
fun33(bt->left);
//printf("%c ", bt->val);
if(pre == ' ') pre = bt->val;
else{
if(pre > bt->val){
isSorted = false;
}else{
pre = bt->val;
}
}
fun33(bt->right);
}
}
bool fun3(BiTree bt){
fun33(bt);
return isSorted;
}
10-4 判断一棵二叉排序树是否为平衡的二叉排序树
bool isBalance = true;
int fun44(BiTree bt){
if(!bt) return 0;
int l = fun44(bt->left);
int r = fun44(bt->right);
if(abs(l-r) > 1){
isBalance = false;
}
return max(l, r)+1;
}
bool fun4(BiTree bt){
fun44(bt);
return isBalance;
}
10-5 求取给定的二叉树的镜像
TreeNode* invertTree(TreeNode* root) {
if(root){
TreeNode* l = invertTree(root->left);
TreeNode* r = invertTree(root->right);
root->left = r;
root->right = l;
return root;
}
return NULL;
}
*10-6 输出二叉树中和为某个值的一个路径
输入一棵二叉树和一个整数,打印出二叉树中节点值的和为输入整数的所有路径。从树的根节点开始往下一直到叶节点所经过的节点形成一条路径。
class Solution {
public:
vector<vector<int>> res;
vector<int> path;
void findPath(TreeNode* root, int tar){
if(!root) return ;
path.push_back(root->val);
tar -= root->val;
if(tar == 0 && root->left == NULL && root->right == NULL){
res.push_back(path);
}
findPath(root->left, tar);
findPath(root->right, tar);
path.pop_back();
}
vector<vector<int>> pathSum(TreeNode* root, int sum) {
findPath( root, sum);
return res;
}
};
*10-7 给定一个数组判断是否为二叉排序树的后序遍历序列
bool fun1(int *a, int start, int end){
if(start >= end) return true;
int mid = start;
while(mid < end && a[mid] < a[end-1]){
mid ++;
}
for( int j = mid; j < end; j ++){
if( a[j] < a[end-1] ){
return false;
}
}
return fun1(a, start, mid) && fun1(a, mid, end-1);
}