链式二叉树
二叉树的创建初始化与销毁
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int BinaryTreeDataType;
typedef struct BinaryTree
{
BinaryTreeDataType x;
struct BinaryTree* left;
struct BinaryTree* right;
}BT;
BT* creatnode(BinaryTreeDataType x)
{
BT* bt = (BT*)malloc(sizeof(BT));
if (bt == NULL)
{
perror("malloc failed");
exit(-1);
}
bt->x = x;
bt->right = NULL;
bt->left = NULL;
return bt;
}
void TreeDestroy(BTNode* root)
{
if (root == NULL)
{
return;
}
TreeDestroy(root->left);
TreeDestroy(root->right);
free(root);
//root = NULL;
}
手动构建二叉树关系
void test()
{
BT* bt1 = creatnode(1);
BT* bt2 = creatnode(2);
BT* bt3 = creatnode(3);
BT* bt4 = creatnode(4);
BT* bt5 = creatnode(5);
BT* bt6 = creatnode(6);
BT* bt7 = creatnode(7);
BT* bt8 = creatnode(8);
BT* bt9 = creatnode(9);
BT* bt10 = creatnode(10);
BT* bt11= creatnode(11);
bt1->left = bt2;
bt1->right = bt4;
bt2->left = bt3;
bt4->left = bt5;
bt4->right = bt6;
bt3->right = bt7;
bt5->left = bt8;
bt6->left = bt9;
bt6->right = bt10;
bt10->left = bt11;
)
二叉树递归前序遍历
void Preorder(BT* bt)
{
if (bt == NULL)
{
printf("NULL ");
return;
}
printf("%d ", bt->x);
Preorder(bt->left);
Preorder(bt->right);
}
二叉树递归中序遍历
void Inorder(BT* bt)
{
if (bt == NULL)
{
printf("NULL ");
return;
}
Inorder(bt->left);
printf("%d ", bt->x);
Inorder(bt->right);
}
二叉树递归后序遍历
void Postorder(BT* bt)
{
if (bt == NULL)
{
printf("NULL ");
return;
}
Postorder(bt->left);
Postorder(bt->right);
printf("%d ", bt->x);
}
二叉树的层序遍历
void Layerordertraversal(BT* root)
{
Que p;
QueueInit(&p);
assert(root);
QueuePush(&p, root);
while (!QueueEmpty(&p))
{
BT* front = QueueFront(&p);
printf("%d ", front->x);
if(front->left!=NULL)
QueuePush(&p, front->left);
if (front->right != NULL)
QueuePush(&p, front->right);
QueuePop(&p);
}
QueueDestroy(&p);
}
计算二叉树节点数量
int TreeSzie(BT* root)
{
if (root == NULL)
return 0;
return TreeSzie(root->left) + TreeSzie(root->right) + 1;
}
计算二叉树叶子节点数量
int TreeLeafSzie(BT* root)
{
if (root == NULL)
return 0;
if (root->left == NULL && root->right == NULL)
return 1;
return TreeLeafSzie(root->left) + TreeLeafSzie(root->right);
}
计算二叉树第K层节点数量
int TreeKLevel(BT* root,int k)
{
assert(k > 0);
if (root == NULL)
return 0;
if (k == 1)
return 1;
return TreeKLevel(root->left,k-1) + TreeKLevel(root->right,k-1);
}
二叉树查找指定值节点
BT* TreeFind(BT* root,BinaryTreeDataType val)
{
if (root == NULL)
return NULL;
if (root->x == val)
return root;
BT* ret = TreeFind(root->left, val);
if (ret)
return ret;
return TreeFind(root->right, val);
}
计算树的高度
int TreeHigh(BT* root)
{
if (root == NULL)
return 0;
int highleft = TreeHigh(root->left) + 1;
int highright = TreeHigh(root->right) + 1;
return highleft > highright ? highleft : highright;
}
测试
void test()
{
BT* bt1 = creatnode(1);
BT* bt2 = creatnode(2);
BT* bt3 = creatnode(3);
BT* bt4 = creatnode(4);
BT* bt5 = creatnode(5);
BT* bt6 = creatnode(6);
BT* bt7 = creatnode(7);
BT* bt8 = creatnode(8);
BT* bt9 = creatnode(9);
BT* bt10 = creatnode(10);
BT* bt11= creatnode(11);
bt1->left = bt2;
bt1->right = bt4;
bt2->left = bt3;
bt4->left = bt5;
bt4->right = bt6;
bt3->right = bt7;
bt5->left = bt8;
bt6->left = bt9;
bt6->right = bt10;
bt10->left = bt11;
Preorder(bt1);
printf("\n");
Inorder(bt1);
printf("\n");
Postorder(bt1);
printf("\n");
printf("%d", TreeSzie(bt1));
printf("\n");
printf("%d",TreeLeafSzie(bt1));
printf("\n");
printf("%d", TreeKLevel(bt1,4));
printf("\n");
printf("%p", TreeFind(bt1, 11));
printf("\n");
printf("%d", TreeHigh(bt1));
printf("\n");
Layerordertraversal(bt1);
TreeDestroy(bt1);
}
int main()
{
test();
return 0;
}
二叉树oj
- 单值二叉树。
如果二叉树每个节点都具有相同的值,那么该二叉树就是单值二叉树。
只有给定的树是单值二叉树时,才返回 true;否则返回 false。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
bool isUnivalTree(struct TreeNode* root){
if(root==NULL)
return true;
if(root->left&&root->left->val!=root->val)
return false;
if(root->right&&root->right->val!=root->val)
return false;
return isUnivalTree(root->left)&&isUnivalTree(root->right);
}
- 检查两颗树是否相同。
给你两棵二叉树的根节点 p 和 q ,编写一个函数来检验这两棵树是否相同。
如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
bool isSameTree(struct TreeNode* p, struct TreeNode* q){
if(p==NULL&&q==NULL)
return true;
if(p==NULL||q==NULL)
return false;
if(p->val!=q->val)
return false;
return isSameTree(p->left,q->left)&&isSameTree(p->right,q->right);
}
- 对称二叉树。
给你一个二叉树的根节点 root , 检查它是否轴对称。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
bool isSametree(struct TreeNode* root1,struct TreeNode* root2)
{
if(root1==NULL&&root2==NULL)
return true;
if(root1==NULL||root2==NULL)
return false;
if(root1->val!=root2->val)
return false;
return isSametree(root1->left,root2->right)&&isSametree(root1->right,root2->left);
}
bool isSymmetric(struct TreeNode* root)
{
return isSametree(root->left,root->right);
}
- 二叉树的前序遍历。
给你二叉树的根节点 root ,返回它节点值的 前序 遍历。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int preorder(struct TreeNode* root)
{
if(root==NULL)
return 0;
return preorder(root->left)+preorder(root->right)+1;
}
void preorderin(struct TreeNode* root,int* arr,int* i)
{
if(root==NULL)
return ;
arr[(*i)++]=root->val;
preorderin(root->left,arr,i);
preorderin(root->right,arr,i);
}
int* preorderTraversal(struct TreeNode* root, int* returnSize){
int n=preorder(root);
int* arr=(int*)malloc(sizeof(int)*n);
int i=0;
preorderin(root,arr,&i);
*returnSize=n;
return arr;
}
- 二叉树的后序遍历 。
给你一棵二叉树的根节点 root ,返回其节点值的 后序遍历 。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int postorder(struct TreeNode* root)
{
if(root==NULL)
return 0;
return postorder(root->left)+postorder(root->right)+1;
}
void postorderin(struct TreeNode* root,int* arr,int* i)
{
if(root==NULL)
return ;
postorderin(root->left,arr,i);
postorderin(root->right,arr,i);
arr[(*i)++]=root->val;
}
int* postorderTraversal(struct TreeNode* root, int* returnSize){
int n=postorder(root);
int* arr=(int*)malloc(sizeof(int)*n);
int i=0;
postorderin(root,arr,&i);
*returnSize=n;
return arr;
}
- 二叉树中序遍历 。
给定一个二叉树的根节点 root ,返回 它的 中序 遍历 。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int inorder(struct TreeNode* root)
{
if(root==NULL)
return 0;
return inorder(root->left)+inorder(root->right)+1;
}
void inorderin(struct TreeNode* root,int* arr,int* i)
{
if(root==NULL)
return ;
inorderin(root->left,arr,i);
arr[(*i)++]=root->val;
inorderin(root->right,arr,i);
}
int* inorderTraversal(struct TreeNode* root, int* returnSize){
int n=inorder(root);
int* arr=(int*)malloc(sizeof(int)*n);
int i=0;
inorderin(root,arr,&i);
*returnSize=n;
return arr;
}
- 另一颗树的子树。
给你两棵二叉树 root 和 subRoot 。检验 root 中是否包含和 subRoot 具有相同结构和节点值的子树。如果存在,返回 true ;否则,返回 false 。
二叉树 tree 的一棵子树包括 tree 的某个节点和这个节点的所有后代节点。tree 也可以看做它自身的一棵子树。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
bool isSametree(struct TreeNode* root, struct TreeNode* subRoot)
{
if(root==NULL&&subRoot==NULL)
return true;
if(root==NULL||subRoot==NULL)
return false;
if(root->val!=subRoot->val)
return false;
return isSametree(root->left,subRoot->left)&&isSametree(root->right,subRoot->right);
}
bool isSubtree(struct TreeNode* root, struct TreeNode* subRoot){
if(root==NULL)
return false;
if(root->val==subRoot->val)
{
if(isSametree(root,subRoot))
return true;
}
return isSubtree(root->left,subRoot)||isSubtree(root->right,subRoot);
}
二叉树的遍历构建与输出
编一个程序,读入用户输入的一串先序遍历字符串,根据此字符串建立一个二叉树(以指针方式存储)。 例如如下的先序遍历字符串: ABC##DE#G##F### 其中“#”表示的是空格,空格字符代表空树。建立起此二叉树以后,再对二叉树进行中序遍历,输出遍历结果。
#include <stdio.h>
#include <stdlib.h>
typedef char BinaryTreeDataType;
typedef struct BinaryTree
{
BinaryTreeDataType x;
struct BinaryTree* left;
struct BinaryTree* right;
}BT;
BT* CreateTree(char* str,int* pj)
{
if(str[*pj]=='#')
{
(*pj)++;
return NULL;
}
BT* root=(BT*)malloc(sizeof(BT));
root->x=str[*pj];
(*pi)++;
root->left=CreateTree(str, pj);
root->right=CreateTree(str, pj);
return root;
}
void inorder(BT* root)
{
if(root==NULL)
return ;
inorder((root->left));
printf("%c ",root->x);
inorder(root->right);
}
int main() {
char arr[100]={};
int i=0;
while(scanf("%c",arr+i)!=EOF)
{
i++;
}
int j=0;
BT* root= CreateTree(arr,&j);
inorder(root);
return 0;
}