- 二叉树的中序遍历
给定一个二叉树的根节点 root ,返回 它的 中序 遍历 。
示例 1:
输入:root = [1,null,2,3]
输出:[1,3,2]
示例 2:
输入:root = []
输出:[]
示例 3:
输入:root = [1]
输出:[1]
提示:
树中节点数目在范围 [0, 100] 内
-100 <= Node.val <= 100
/**
* 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* returnSize,int *res){
if(root==NULL){
return ;
}
inorder(root->left,returnSize,res);
res[(*returnSize)++]=root->val;
inorder(root->right,returnSize,res);
}
int* inorderTraversal(struct TreeNode* root, int* returnSize) {
int *res = (int *)malloc(sizeof(int)*501);//returnSize是表示返回的数组大小
*returnSize=0;
inorder(root,returnSize,res);
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().
*/
void pre(struct TreeNode* root, int* returnSize,int *res){
if(root==NULL){
return ;
}
res[(*returnSize)++]=root->val;
pre(root->left,returnSize,res);
pre(root->right,returnSize,res);
}
int* preorderTraversal(struct TreeNode* root, int* returnSize){
int *res = (int *)malloc(sizeof(int)*501);
*returnSize=0;
pre(root,returnSize,res);
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().
*/
void post(struct TreeNode* root, int* returnSize,int *res){
if(root==NULL){
return;
}
post(root->left,returnSize,res);
post(root->right,returnSize,res);
res[(*returnSize)++]=root->val;
}
int* postorderTraversal(struct TreeNode* root, int* returnSize){
int *res=(int *)malloc(sizeof(int)*501);
*returnSize=0;
post(root,returnSize,res);
return res;
}
层序遍历:
/**
* 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){
int** ans=(int**)malloc(sizeof(int*)*2000);
*returnSize=0;
if(!root) return NULL;//要在return前给出*returnSize的取值不然过不去;!!
int columnSizes[2000];//记录每一行的列数(每层结点数),因为要给出* returnColumnSizes
struct TreeNode* queue[2000];//模拟队列
int rear=0;int head=0;//记录队列头尾
queue[rear++]=root;//录入根结点
while(rear!=head){//队列不为空
ans[(*returnSize)]=(int*)malloc(sizeof(int)*(rear-head));
columnSizes[(*returnSize)]=rear-head;
int start=head;//记录遍历开始位置,即为本层的头
head=rear;//本层的尾部成为下次的头,因为所有的元素均弹出队列
//在这里下层的头head同时作为遍历结束的位置,因为在遍历中rear会不断改变,成为下层的尾
for(int i=start;i<head;i++){//弹出start到未变化的rear(即为head)之间的所有元素
ans[(*returnSize)][i-start]=queue[i]->val;
if(queue[i]->left) queue[rear++]=queue[i]->left;
if(queue[i]->right) queue[rear++]=queue[i]->right;//rear不断改变
}
(*returnSize)++;//一层遍历完*returnSize加一;
}
*returnColumnSizes=(int*)malloc(sizeof(int)*(*returnSize));//给出*returnColumnSizes
for(int i=0;i<*returnSize;i++) (*returnColumnSizes)[i]=columnSizes[i];
return ans;
}
二叉树的各种遍历实现
1920

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



