leetcode【数据结构简介】《N叉树》卡片 - 遍历

Authur Whywait 做一块努力吸收知识的海绵
想看博主的所有leetcode卡片学习笔记传送门点这儿

《N叉树》卡片将把我们关于二叉树的认知衍生到N叉树 (N - ary Tree) 。

遍历

对应于二叉树的四种遍历方式:前序遍历,中序遍历,后序遍历以及层序遍历(详情请点击传送门),N叉树也有。

不过对于中序遍历来说,只有在二叉树中有明确定义。而且N叉树的中序遍历无标准定义,实践中也不常用到,所以我们跳过N叉树的中序遍历部分。

具体遍历方式

  1. 前序遍历:在N叉树中,先访问根节点,然后逐个遍历以其子节点为根的子树。
  2. 后序遍历:在N叉树中,先逐个遍历以根节点的子节点为根的子树,最后访问根节点。
  3. 层序遍历:N叉树的层序遍历与二叉树的一致。在树中进行广度优先搜索时,我们按照层序的顺序进行遍历。

接下来是相关编程练习~

N叉树的前序遍历🚩

给定一个 N 叉树,返回其节点值的前序遍历。

递归法

# define SIZE 100000

void preorderNtree(struct Node* root, int* nums, int* returnSize){
    if(!root) return;
    nums[(*returnSize)++] = root->val;
    for(int i=0; i<root->numChildren; i++) preorderNtree(root->children[i], nums, returnSize);
}

int* preorder(struct Node* root, int* returnSize) {
    * returnSize = 0;
    if(!root) return NULL;
    int* nums = (int *)malloc(sizeof(int) * SIZE);
    preorderNtree(root, nums, returnSize);
    return nums;
}

类似二叉树前序遍历的递归,套用之前程序模板即可。
在这里插入图片描述

N叉树的后序遍历🚩

给定一个 N 叉树,返回其节点值的后序遍历。

递归法

# define SIZE 100000

void postorderNtree(struct Node* root, int* nums, int* returnSize){
    if(!root) return;
    for(int i=0; i<root->numChildren; i++) postorderNtree(root->children[i], nums, returnSize);
    nums[(*returnSize)++] = root->val;
}


int* postorder(struct Node* root, int* returnSize) {
    * returnSize = 0;
    if(!root) return NULL;
    int* nums = (int *)malloc(sizeof(int) * SIZE);
    postorderNtree(root, nums, returnSize);
    return nums;
}

在这里插入图片描述

N叉树的层次遍历🚩

给定一个 N 叉树,返回其节点值的层序遍历。 (即从左到右,逐层遍历)。

分析

只需对二叉树的层次遍历([详情请点击传送门]的代码略作修改即可(https://blog.csdn.net/AuthurWhywat/article/details/105253887))

BFS算法

/*
 * @lc app=leetcode.cn id=429 lang=c
 *
 * [429] N叉树的层序遍历
 */

/**
 * 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().
 */
// typedef struct Node{
//     elementtype elementname;
// } name, * pointer;


// struct Node {
//     int val;
//     int numChildren;
//     struct Node** children;
// };
// # include<stdlib.h>

# define DEEP 100000
# define NODE 500000

int** levelOrder(struct Node* root, int* returnSize, int** returnColumnSizes) {
    * returnSize = 0;
    * returnColumnSizes = (int *)malloc(sizeof(int) * NODE);
    * returnColumnSizes = 0;
    if(!root) return NULL;
    int **array = (int **)malloc(sizeof(int*) * DEEP);

    struct Node* queue = (struct Node *)malloc(sizeof(struct Node) * NODE);
    int front=0, tail=0;
    * returnColumnSizes = (int *)malloc(sizeof(int) * NODE);

    array[* returnSize] = (int *)malloc(sizeof(int));
    array[* returnSize][0] = root->val;
    (* returnColumnSizes)[* returnSize] = 1;
    queue[tail++] = *root;
    
    while(front<tail){
        (* returnSize)++;
        int counter=0;
        int size=tail-front;
        array[* returnSize] = (int *)malloc(sizeof(int) * NODE);
        for(int i=0; i<size; i++){
            if(!queue[front].numChildren){
                front++; 
                continue;
            }
            for(int j=0; j<queue[front].numChildren; j++){
                queue[tail++] = *(queue[front].children[j]);
                array[* returnSize][counter++] = (*(queue[front].children[j])).val;
            }
            front++;
        }
        (* returnColumnSizes)[* returnSize] = counter;
    }
    return array;
}

在这里插入图片描述

都看到这里了,确定不点个赞再走?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AuthurLEE

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值