文章目录
- 二叉树遍历
- 先序遍历
- 中序遍历
- 后序遍历
- 层序遍历
- 层序遍历Ⅱ
- 二叉树的右视图
- 二叉树的层平均值
- N插树的层序遍历
- 在每个树行中找最大值
- 填充每个节点的下一个右侧节点指针
- 填充每个节点的下一个右侧节点指针 II
二叉树遍历
先序遍历
二叉树先序遍历
递归形式
/*** 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 ans[100];
int cnt=0;
void PreOrder(struct TreeNode* root){if(root==NULL)return;ans[cnt++]=root->val;PreOrder(root->left);PreOrder(root->right);
}
int* preorderTraversal(struct TreeNode* root, int* returnSize) {cnt=0;PreOrder(root);*returnSize=cnt;return ans;
}
非递归形式(迭代法)
/*** 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* preorderTraversal(struct TreeNode* root, int* returnSize) {int *ans=malloc(sizeof(int)*100);*returnSize=0;if(root==NULL)return ans;struct TreeNode* st[100];struct TreeNode* node=root;int top=0;while(top>0||node!=NULL){while(node!=NULL){ans[(*returnSize)++]=node->val;st[top++]=node;node=node->left;}node=st[--top]->right;}return ans;
}
中序遍历
二叉树中序遍历
递归
/*** 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().*/
void Inorder(struct TreeNode* root,int *ans,int *cnt){if(root==NULL)return;Inorder(root->left,ans,cnt);ans[(*cnt)++]=root->val;Inorder(root->right,ans,cnt);
}
int* inorderTraversal(struct TreeNode* root, int* returnSize) {int *ans=malloc(sizeof(int)*100);int cnt=0;Inorder(root,ans,&cnt);*returnSize=cnt;return ans;
}
非递归
/*** 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* inorderTraversal(struct TreeNode* root, int* returnSize) {int *ans=malloc(sizeof(int)*100);*returnSize=0;if(root==NULL)return ans;struct TreeNode *st[100];struct TreeNode *node=root;int top=0;while(top>0||node!=NULL){while(node!=NULL){st[top++]=node;node=node->left;}node=st[--top];ans[(*returnSize)++]=node->val;node=node->right;}return ans;
}
后序遍历
二叉树后序遍历
递归
/*** 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().*/
void PostOrder(struct TreeNode* root,int *ans,int *cnt){if(root==NULL)return;PostOrder(root->left,ans,cnt);PostOrder(root->right,ans,cnt);ans[(*cnt)++]=root->val;
}
int* postorderTraversal(struct TreeNode* root, int* returnSize) {int *ans=malloc(sizeof(int)*100);int cnt=0;PostOrder(root,ans,&cnt);*returnSize=cnt;return ans;
}
非递归
/*** 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* postorderTraversal(struct TreeNode* root, int* returnSize) {int *ans=malloc(sizeof(int)*100);*returnSize=0;if(root==NULL)return ans;struct TreeNode* st[100];struct TreeNode* node=root;struct TreeNode* pre=NULL;int top=0;while(top>0||node!=NULL){while(node!=NULL){st[top++]=node;node=node->left;}node=st[--top];if(node->right==NULL||node->right==pre){ans[(*returnSize)++]=node->val;pre=node;node=NULL;}else{st[top++]=node;node=node->right;}}return ans;
}
层序遍历
二叉树层序遍历
/*** Definition for a binary tree node.* struct TreeNode {* int val;* struct TreeNode *left;* struct TreeNode *right;* };*/
/*** Return an array of arrays of size *returnSize.* The sizes of the arrays are returned as *returnColumnSizes array.* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().*/
int** levelOrder(struct TreeNode* root, int* returnSize, int** returnColumnSizes) {//returnSize——二叉树高度//returnColumnSizes——每层的节点数struct TreeNode *queue[2000];int front=0;int tail=0;int **ans=malloc(sizeof(int*)*2000);*returnColumnSizes=malloc(sizeof(int)*2000);queue[front++]=root;*returnSize=0;if(root==NULL)return ans;while(front>tail){int t=front;int cnt=0;//记录该层节点个数ans[*returnSize]=malloc(sizeof(int)*(t-tail));for(tail;tail<t;tail++){//遍历当前层的节点,并将下一层节点放入struct TreeNode* node=queue[tail];ans[*returnSize][cnt++]=node->val;if(node->left)queue[front++]=node->left;if(node->right)queue[front++]=node->right;}(*returnColumnSizes)[(*returnSize)++]=cnt;}return ans;
}
层序遍历Ⅱ
二叉树层序遍历Ⅱ
思路:与上面的思路类似,只是在后面把结果反转
/*** Definition for a binary tree node.* struct TreeNode {* int val;* struct TreeNode *left;* struct TreeNode *right;* };*/
int** levelOrderBottom(struct TreeNode* root, int* returnSize, int** returnColumnSizes) {if (root == NULL) {*returnSize = 0;*returnColumnSizes = NULL;return NULL;}struct TreeNode** queue = malloc(sizeof(struct TreeNode*) * 2000); // 动态分配队列int front = 0, tail = 0;int** ans = malloc(sizeof(int*) * 2000); // 存储层次遍历结果*returnColumnSizes = malloc(sizeof(int) * 2000); // 存储每层的节点数*returnSize = 0;queue[front++] = root; // 根节点入队while (front > tail) {int levelSize = front - tail; // 当前层的节点数ans[*returnSize] = malloc(sizeof(int) * levelSize); // 分配当前层的存储空间(*returnColumnSizes)[*returnSize] = levelSize; // 记录当前层的节点数for (int i = 0; i < levelSize; i++) {struct TreeNode* node = queue[tail++];ans[*returnSize][i] = node->val; // 存储当前节点的值if (node->left) queue[front++] = node->left; // 左子节点入队if (node->right) queue[front++] = node->right; // 右子节点入队}(*returnSize)++; // 处理完一层}// 反转结果int** res = malloc(sizeof(int*) * (*returnSize));for (int i = 0; i < *returnSize; i++) {int level = *returnSize - 1 - i;res[i] = ans[level]; // 直接使用 ans 的内存,避免重复分配}// 反转 returnColumnSizesfor (int i = 0; i < *returnSize / 2; i++) {int temp = (*returnColumnSizes)[i];(*returnColumnSizes)[i] = (*returnColumnSizes)[*returnSize - 1 - i];(*returnColumnSizes)[*returnSize - 1 - i] = temp;}free(queue); // 释放队列内存return res;
}
二叉树的右视图
题目链接
思路:其实就是取每一层的最右边节点的值,在层序遍历时只保存最后一个值就行了。
/*** 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* rightSideView(struct TreeNode* root, int* returnSize) {
//其实就是取每一层最右边的值struct TreeNode *queue[100];int *ans=malloc(sizeof(int)*100);struct TreeNode* node=root;*returnSize=0;int front=0;int tail=0;queue[front++]=node;if(root==NULL)return ans;while(front>tail){int t=front;while(tail<t){node=queue[tail++];if(node->left)queue[front++]=node->left;if(node->right)queue[front++]=node->right;}ans[(*returnSize)++]=queue[t-1]->val;} return ans;
}
二叉树的层平均值
题目链接
思路:层次遍历的时候计算计算一下平均值
/*** 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().*/
double* averageOfLevels(struct TreeNode* root, int* returnSize) {double *ans=malloc(sizeof(int)*10005);struct TreeNode* queue[10005];int front=0,tail=0;struct TreeNode *node=root;*returnSize=0;queue[front++]=node;if(root==NULL)return ans;while(front>tail){double avg=0;int t=front;double num=front-tail;//当前层节点数while(tail<t){node=queue[tail++];if(node->left)queue[front++]=node->left;if(node->right)queue[front++]=node->right;avg+=(double)node->val;}avg/=num;ans[(*returnSize)++]=avg;}return ans;
}
N插树的层序遍历
题目链接
思路:paper tiger,和二叉树的层次遍历的区别在于访问孩子节点的方式不同
/*** Definition for a Node.* struct Node {* int val;* int numChildren;* struct Node** children;* };*//*** Return an array of arrays of size *returnSize.* The sizes of the arrays are returned as *returnColumnSizes array.* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().*/
int** levelOrder(struct Node* root, int* returnSize, int** returnColumnSizes) {struct Node* queue[10005];int front=0,tail=0;int **ans=malloc(sizeof(int*)*1000);struct Node* node=root;*returnSize=0;*returnColumnSizes=malloc(sizeof(int)*1000);if(root==NULL)return ans;queue[front++]=node;while(front>tail){int len=front-tail;ans[*returnSize]=malloc(sizeof(int)*len);int cnt=0;//保存访问到该层的第几个节点int t=front;while(tail<t){node=queue[tail++];ans[*returnSize][cnt++]=node->val;if(node->numChildren>0){//将孩子节点放入队列 int n=node->numChildren;for(int i=0;i<n;i++){queue[front++]=(node->children)[i];}}}(*returnColumnSizes)[(*returnSize)++]=len;}return ans;
}
在每个树行中找最大值
题目链接
思路:由上面求平均值改写,将求平均值的逻辑改为求最大值就行啦! >v<
/*** 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* largestValues(struct TreeNode* root, int* returnSize) {int *ans=malloc(sizeof(int)*10005);struct TreeNode* queue[10005];int front=0,tail=0;struct TreeNode *node=root;*returnSize=0;queue[front++]=node;if(root==NULL)return ans;while(front>tail){int Max=queue[tail]->val;int t=front;while(tail<t){node=queue[tail++];if(node->left)queue[front++]=node->left;if(node->right)queue[front++]=node->right;if(node->val>Max)Max=node->val;}ans[(*returnSize)++]=Max;}return ans;
}
填充每个节点的下一个右侧节点指针
题目链接
思路:还是二叉树层序遍历的思路,每一层的节点用next指针连接就好,不要忘记边界哦。
/*** Definition for a Node.* struct Node {* int val;* struct Node *left;* struct Node *right;* struct Node *next;* };*/struct Node* connect(struct Node* root) {//实际上只需要填充next指针即可struct Node* queue[5000];int front=0;int tail=0;if(root==NULL)return root;queue[front++]=root;while(front>tail){int t=front;while(tail<t){if(queue[tail]->left)queue[front++]=queue[tail]->left;if(queue[tail]->right)queue[front++]=queue[tail]->right;if(tail<t-1)queue[tail]->next=queue[tail+1];else queue[tail]->next=NULL;tail++;}}return root;;
}
填充每个节点的下一个右侧节点指针 II
填充每个节点的下一个右侧节点指针 II
思路:直接用上一题代码就行了,但是要把队列内存扩大。。。hahaha
/*** Definition for a Node.* struct Node {* int val;* struct Node *left;* struct Node *right;* struct Node *next;* };*/struct Node* connect(struct Node* root) {struct Node* queue[6005];int front=0;int tail=0;if(root==NULL)return root;queue[front++]=root;while(front>tail){int t=front;while(tail<t){if(queue[tail]->left)queue[front++]=queue[tail]->left;if(queue[tail]->right)queue[front++]=queue[tail]->right;if(tail<t-1)queue[tail]->next=queue[tail+1];else queue[tail]->next=NULL;tail++;}}return root;;
}