title:: 二叉树/算法题
tags:: 算法题
- 求按顺序结构存储,编号为i,j的最近公共祖先
-
int findNearestAns(int bTree[], int i, int j){ while(i!=j){ if(i>j){ i = i / 2; }else{ j = j / 2; } } return bTree[i]; }
-
- 非递归求二叉树高度
-
/** * 利用层次遍历求高度 * @param T 二叉树 * @return 高度 */ int levelOrder_Depth(BiTree T) { if (T == NULL) return 0; SeqQueue q; BiTree p = T; BiNode *last = T; int depth = 0; InitQueue(q); EnQueue(q, T); while (!IsQueueEmpty(q)) { DeQueue(q, p); //visit(p); if (!p->lchild) { EnQueue(q, p->lchild); } if (!p->rchild) { EnQueue(q, p->rchild); } if (p == last) { depth++; last = p->rchild; } } return depth; } /** * 利用非递归后序遍历求二叉树高度 * @param T 二叉树 * @return 二叉树高度 */ int postOrderLeve(BiTree T) { if (T == NULL) return 0; SqStack s; BiNode *pre = NULL; BiTree p = T; int max = 1, depth; while (p || !IsStackEmpty(s)) { if (p) { Push(s, p); p = p->lchild; } else { GetTop(s, p); if (p->rchild && p->rchild != pre) { Push(s, p); p = p->rchild; } else { Pop(s, p); depth = getStackSize(s); if (depth > max) max = depth; pre = p; p = NULL; } } } return max; }
-
- 递归求二叉树高度
-
int getTreeHight(bTree T){ if(!T) return 0; else{ int m = getTreeHight(T->lchild); int n = getTreeHight(T->rchild); return m>n?m+1:n+1; } }
-
- 由先序序列和中序序列构建二叉树
-
bTree createBTree(int A[], int B[],int preFirst, int preLast, int inFirst, int inLast){ preFirst = inFirst = 1; preLast = inLast = n; int i; bTNode *root = (bTNode*)malloc(sizeof(bTNode)); for(i = inFirst;B[i] != A[preFirst]; i++); int lLen = i - inFirst; int rLen = inLast - i ; if(lLen) root->lchild = createBTree(A,B,preFirst+1, preFirst + lLen - 1, inFirst, i-1 ); else root->child = NULL; if(rLen) root->rchild = createBTree(A,B,preFirst+lLen,preLast,i+1,inLast); else root->rchild =NULL; return root; }
-
- 判断二叉链表存储的二叉树是否是完全二叉树
-
bool isFullBTree(bTree T){ if(T == NULL) return true; bTree p = T; int tree[MAXSIZE]; for(int i = 0; i < MAXSIZE; i++) tree[i] = 0; int i = 1; int j = 0; queue q; initial(q); enQueue(q,p); tree[i] = 1; while(!queueEmpty(q)){ deQueue(q,p); if(p->lchild){ enQueue(q,p->lchild); tree[2*i] = 1; } if(p->rchild){ enQueue(q,p->rchild); tree[2*i+1] = 1; } i++; } bool tag = true; for(j = 0; j < i;j++){ if(tree[j] == 0) tag = false; if(tag == false && tree[j] == 1) return false; } return true; } /** 均为利用层次遍历,但是更简洁的做法 **/ bool isFullBTree(bTree T){ if(T == NULL) return true; queue q; bTree p = T; initial(q); enQueue(q,p); while(!queueEmpty(q)){ deQueue(q,p); if(p){ enQueue(q,p->lchild); enQueue(q,p->rchild); }else{ while(!queueEmpty(q)){ deQueue(q,p); if(p) return false; } } } return true; }
-
- 求二叉链表存储的二叉树的双分支结点个数
- 本质上就是对二叉树进行一次遍历,并统计其中出现的双分支结点个数,所以四种遍历方式均可使用
-
int numberOfHasChildren(bTree T){ if(T == NULL) return 0; queue q; bTree p = T; initial(q); enQueue(q,p); int num = 0; while(!emptyQueue(q)){ deQueue(q,p); if(p->lchild&&p->rchild) num ++; if(p->lchild) enQueue(q,p->lchild); if(p->rchild) enQueue(q,p->rchild); } return num; } int num == 0; int numberOfHasChildren_2(bTree T){ if(T == NULL) return 0; if(T->lchild && T->rchild) num++; numberOfHasChildren_2(T->lchild); numberOfHasChildren_2(T->rchild); } int numberOfHasChildren_3(bTree T){ if(T == NULL) return 0; if(T->lchild && T->rchild) return numberOfHasChildren_3(T->lchild) + numberOfHasChildren_3(T->rchild) + 1; else return numberOfHasChildren_3(T->lchild) + numberOfHasChildren_3(T->rchild); }
- 交换二叉链表存储的二叉树左右结点
-
int swapTree(bTree T){ if(T == NULL) return 0; BtNode *temp = T->lchild; T->lchild = T->rchild; T->rchild = temp; swapTree(T->lchild); swapTree(T->rchild); }
-
- 求先序遍历第k个结点的值
-
int i = 0 int preOrder(bTree T, int k, char &ch){ if(T){ i++; if(i == k){ ch = T->data; return 1; } }else{ return 0; } preOrder(T->lchild,k); preOrder(T->rchild,k); } int j = 0; elemType preOrder_1(bTree T, int k){ elemType ch; if(T == NULL) return '#'; j++; if(j == k) return T->data; ch = preOrder_1(T->lchild,k); if(ch!='#') return ch; ch = preOrder_1(T->rchild,k); reurn ch; } - 错误代码,在右子树最后一个结点的右孩子为空,向主函数返回KaTeX parse error: Expected 'EOF', got '#' at position 1: #̲后,递归调用栈中的函数都会依次结束,而不返回新的值,因为此时它们已经执行到了第11行,也即先序遍历遍历右子树,并不会返回应该找到的data值,也可以说:有一条控制路径上没有返回值,即true、false路径上函数没有返回值;而且,由于最后需要找到结点的数据值,而该算法有可能在途中就找到了这个值,此时这个值就无法被保存下来,会被后续的返回值刷新
-
char wrFind(BiTree T,int k){ if(T){ j++; if(j == k){ return T->data; } }else{ return '#'; } wrFind(T->lchild,k); wrFind(T->rchild,k); }
-
-
- 删除树中以值为x的结点的子树
-
int DeleteTree(BiTree &T){ if(T){ DeleteTree(T->lchild); DeleteTree(T->rchild); free(T); return 1; } return 0; } int SearchNodesByElem(BiTree T, ELemType x){ if (T){ if(T->data == x){ DeleteTree(T); return 1; } BiTree Q[MAXSIZE]; BiTree p; int front,rear; front = rear = 0; Q[rear] = T; rear = (rear + 1)%MAXSIZE; while (front != rear){ p = Q[front]; front = (front+1)%MAXSIZE; if (p->lchild){ if(p->lchild->data == x){ DeleteTree(p->lchild); p->lchild = NULL; }else{ Q[rear] = p->lchild; rear = rear+1%MAXSIZE; } } if (p->rchild){ if(p->rchild->data == x){ DeleteTree(p->rchild); p->rchild = NULL; }else{ Q[rear] = p->rchild; rear = rear + 1%MAXSIZE; } } } return 1; } else{ return 0; } }
-
- 找到值为x的结点的所有祖先(该题x不多于一个,假设多于一个,也能按照本题做)
-
typedef struct { BiTree t; int tag; }TreeStack; int PostOrderFindAncestors(BiTree T,ELemType x){ if(T == NULL) return 0; TreeStack s[MAXSIZE]; int top = 0; BiTree p = T; while (p || top != 0){ while (p && p->data != x){ s[top].t = p; s[top].tag = 0; top ++; p = p->lchild; } if(p && p->data == x){ printf("该结点的所有祖先为:"); for (int j = top; j < 0; --j) { printf("%c",s[j].t->data); } return 1; } while(top != 0 && s[top].tag == 1) top--; if (top!= 0){ p = s[top].t->rchild; s[top].tag = 1; } } }
-
- 找到两个结点的最近公共祖先
-
/** * 找到p,q最近的共同祖先 * @param T 树 * @param p * @param q * @return 最近的公共祖先 */ BiNode *ANCESTOR(BiTree T, BiNode *p, BiNode *q){ TreeStack sp[MAXSIZE],sq[MAXSIZE]; int topP = 0,topQ = 0; BiNode *temp = p; while (temp || topP != 0){ if(temp){ sp[topP].t = temp; sp[topP].tag = 0; temp = temp->lchild; topP++; }else{ temp = sp[topP].t; if(temp->rchild && sp[topP].tag != 1){ temp = temp->rchild; } else{ temp = sp[--topP].t; if(p == temp) break; temp = NULL; } } } temp = q; while (temp || topQ != 0){ if(temp){ sq[topQ].t = temp; sq[topQ].tag = 0; temp = temp->lchild; topQ++; }else{ temp = sq[topQ].t; if(temp->rchild && sq[topQ].tag != 1){ temp = temp->rchild; } else{ temp = sq[--topQ].t; if(q == temp) break; temp = NULL; } } } int top = topP > topQ ? topQ : topP; if (top != 0) { for (int j = top; j > 0; --j) { if (sp[top].t == sq[top].t) return sp[top].t; } } return NULL; }
-
- 层次遍历求宽度
- 和求高度用一个指针记录上一层的最后一个结点有异曲同工之妙
-
/** * 层次遍历求非空二叉树的宽度 * @param T 二叉树的根结点 * @return 二叉树宽度 */ int WidthOfBiTree(BiTree T) { BiTree Q[MAXSIZE]; int front = 0, rear = 0; BiNode *p = T; BiNode *last = p; // 记录每一层遍历的最后一个结点 int MaxWidth = 1, tempWidth = 0; if (!T) { printf("空树"); return 0; } Q[rear] = T; rear = (rear + 1) % MAXSIZE; while (front != rear) { p = Q[front]; front = (front + 1) % MAXSIZE; tempWidth++; // 当前结点就是本层最后一个结点,说明该层结点已经全部处理完毕 if (p == last) { last = p->rchild; if (tempWidth > MaxWidth) MaxWidth = tempWidth; tempWidth = 0; } //左右子树入队 if (p->lchild) { Q[rear] = p->lchild; rear = (rear + 1) % MAXSIZE; } if (p->rchild) { Q[rear] = p->rchild; rear = (rear + 1) % MAXSIZE; } } return MaxWidth; } /** * 层次遍历求宽度 * 用len记录上层结点遍历完成时,即下一层结点全部进入队列 * 此时的队列的长度即为下一层的宽度,比较得出最大宽度 * @param T * @return */ int WidthOfBiTree_1(BiTree T) { BiTree Q[MAXSIZE]; int front = 0, rear = 0; int width = 1; //最大宽度 int level = 1; int max_Level = 1;//最大宽度层次 BiNode *p = T; int len = 1; //上层队列长度 Q[rear] = T; rear = (rear + 1) % MAXSIZE; while (rear != front) { p = Q[front]; front = (front + 1) % MAXSIZE; len--; //左右子树入队 if (p->lchild) { Q[rear] = p->lchild; rear = (rear + 1) % MAXSIZE; } if (p->rchild) { Q[rear] = p->rchild; rear = (rear + 1) % MAXSIZE; } //长度为0说明此时本层结点遍历完毕, //需要进入下层,而此时队列中元素个数即为下层元素个数 if (len == 0) { len = (rear - front + MAXSIZE) % MAXSIZE; level++; if (len > width) { width = len; max_Level = level; } } } printf("最大宽度所在层次:%d", max_Level); return width; }
- 由二叉树的度表示法所构成的后序序列构造二叉树
- 利用队列求所有结点路径(层次遍历)
-
/** * 使用c++自带的stl寻找二叉树的所有路径 * @param root 二叉树根结点 * @return 返回包含所有路径的向量 */ vector<string> BiTreePath(BiTree root) { vector<string> paths; if(root == nullptr){ return paths; } queue<BiNode*> node_queue; queue<string> path_queue; //根结点入队 node_queue.push(root); path_queue.push(to_string(root->data)); //基于层次遍历 while (!node_queue.empty()){ BiNode* node = node_queue.front(); string path = path_queue.front(); node_queue.pop(); path_queue.pop(); //到达叶结点,路径也到达终点,可以直接加入结果向量 if(node->lchild == nullptr && node->rchild == nullptr){ paths.push_back(path); } else{ //处理分支结点 if(node->lchild){ node_queue.push(node->lchild); path_queue.push(path + "->"+to_string(node->lchild->data)); } if(node->rchild){ node_queue.push((node->rchild)); path_queue.push(path + "->" + to_string(node->rchild->data)); } } } return paths; }
425



被折叠的 条评论
为什么被折叠?



