二叉樹,2月24日(補)

2021-10-21 02:19:33 字數 4409 閱讀 2502

二叉樹的**實現,及二叉樹相關函式的實現

#include#include#define n 50

#define max 30

struct treenode

;struct node

;struct queue

;struct node *createnode1(struct treenode *root)

newnode->next = null;

newnode->root = root;

return newnode;

}void push(struct queue *qp,struct treenode *root)

if(qp->size == 0)

else

qp->size++;

}struct treenode *pop(struct queue *qp)

struct node *ptr = qp->front;

qp->front = ptr->next;

qp->size--;

struct treenode *p = ptr->root;

free(ptr);

return p;

}void init(struct treenode **root)

struct treenode *createnode(int value)

newnode->left = null;

newnode->right = null;

newnode->value = value;

return newnode;

}int createtree(struct treenode **root, char *string)

top++;

s[top] = p;

flag = 1;

break;

case ')': //右子樹結束

if(top == -1)

top --;

break;

case ',': //右子樹出現

flag = 2;

break;

default: //新的節點出現了

p = createnode(*(string + i));

if(p == null)

if(*root == null)

elseelse

} break;

} i++;

}}

void preprint(struct treenode *root)

}void midprint(struct treenode *root)

}void tailprint(struct treenode *root)

}int getnum(struct treenode *root)

return getnum(root->left) + getnum(root->right) + 1;

}int gethigh(struct treenode *root)

int lefthigh = gethigh(root->left);

int righthigh = gethigh(root->right);

if(lefthigh > righthigh)

else

}int gethighnode(struct treenode *root, int k)

if(k < 1)

if(k == 1)

int leftnum = gethighnode(root->left, k - 1);

int rightnum = gethighnode(root->right, k - 1);

return leftnum + rightnum;

}int getleafnum(struct treenode *root)

if((root->left == null) && (root->right == null))

int leftnum = getleafnum(root->left);

int rightnum = getleafnum(root->right);

return leftnum + rightnum;

}struct treenode *mirror(struct treenode *root)

struct treenode *leftroot = mirror(root->left);

struct treenode *rightroot = mirror(root->right);

root->left = rightroot;

root->right = leftroot;

return root;

}int structcmp(struct treenode *root1, struct treenode *root2)

else if(root1 == null || root2 == null) //乙個為空,乙個不為空

int leftcmp = structcmp(root1->left, root2->left);

int rightcmp = structcmp(root1->right, root2->right);

return leftcmp && rightcmp;

}int structtrue(struct treenode *root1, struct treenode *root2)

else

return 0;

}int iscomplatetree(struct treenode *root)

struct queue queue1 = ;

push(&queue1, root); //根節點入佇列

int leaf = 0;

while(queue1.size != 0) //佇列沒有結束

}elseelse if(p->left != null && p->right == null)//左不為空,右空

else if(p->left == null && p->right != null)//左孩子空,右不為空

} }

return 1;

}int main()

else

return 0;

}

二叉樹的增刪查操作

#include#includestruct treenode

;struct queue

;void midprint(struct treenode *root)

}struct treenode *createnode(int value)

newnode->left = null;

newnode->right = null;

newnode->value = value;

return newnode;

}void init(struct treenode **root, int dat)

}void insert(struct treenode *root, int data)

int rootdata = root->value;

if(root->left == null && data < rootdata)//左孩子為空,插入較小值

if(root->right == null && data > rootdata)//右孩子為空,插入較大值

if(data > rootdata)

else

}struct treenode * search(struct treenode *root, int value)

if(root->value == value)

else if(root->value right, value); //遞迴查詢右子樹

}else if(root->value > value)

}void delete(struct treenode *root, int value)

elseelse if(target->left == null) //左子樹為空

else

target->value = q->value;

if(p != target)

else

free(q);

} }

}int main()

; struct treenode *root;

init(&root, a[0]);

for(i = 0; i < 10; i++)

midprint(root);

return 0;

}

二叉樹24 重建二叉樹

題目 輸入某二叉樹的前序遍歷和中序遍歷的結果,請重建出該二叉樹。假設輸入的前序遍歷和中序遍歷的結果中都不含重複的數字。例如輸入前序遍歷序列和中序遍歷序列,則重建二叉樹並返回。思路 已知一棵樹的先序遍歷的結果陣列和中序遍歷的結果,要求據此重建一棵二叉樹,即重建所有結點並設定結點之間的指標關係,最後返回...

110 平衡二叉樹 8月17日

110.平衡二叉樹 遞迴解決 後續遍歷,先得到當前節點兩棵子樹的高度,比較是否滿足條件 返回較大值作為當前節點的高度。每個節點作為根的高度之差 definition for a binary tree node.struct treenode class solution return max h1...

24 平衡二叉樹(簡單)

給定乙個二叉樹,判斷它是否是高度平衡的二叉樹。本題中,一棵高度平衡二叉樹定義為 乙個二叉樹每個節點 的左右兩個子樹的高度差的絕對值不超過1。示例 1 給定二叉樹 3,9,20,null,null,15,7 3 9 20 15 7 返回 true 示例 2 給定二叉樹 1,2,2,3,3,null,n...