首页 > 其他分享 >牛客网——实现二叉树先序、中序和后序遍历

牛客网——实现二叉树先序、中序和后序遍历

时间:2023-04-22 14:26:08浏览次数:47  
标签:count int 中序 牛客 二叉树 nodes root order result

title: 牛客网——实现二叉树先序、中序和后序遍历

题目描述:

分别按照二叉树先序,中序和后序打印所有的节点。

示例:

输入:

{1,2,3}

返回值:

[[1,2,3],[2,1,3],[2,3,1]]

备注:
$$
n \leqslant 10^6
$$
代码如下:

(照着别人的代码敲的,待重新实现一遍)

/**
 * struct TreeNode {
 *	int val;
 *	struct TreeNode *left;
 *	struct TreeNode *right;
 * };
 */

/*前序遍历实现节点计数器,计算了以当前节点为根节点的子树的节点总数*/
void get_nodeNum(struct TreeNode* root, int* count)
{
    ++(*count);
    if(root->left){
        get_nodeNum(root->left, count);
    }
    if(root->right){
        get_nodeNum(root->right, count);
    }    
}

/*前序遍历*/
void pre_order_traversal(struct TreeNode* root, int* nodes, int* count)
{
    nodes[(*count)++] = root->val;
    if(root->left){
        pre_order_traversal(root->left, nodes, count);
    }
    if(root->right){
        pre_order_traversal(root->right, nodes, count);
    }
}

/*中序遍历*/
void in_order_traversal(struct TreeNode* root, int* nodes, int* count)
{
    if(root->left){
        in_order_traversal(root->left, nodes, count);
    }
    nodes[(*count)++] = root->val;
    if(root->right){
        in_order_traversal(root->right, nodes, count);
    }
}

/*后续遍历*/
void post_order_traversal(struct TreeNode* root, int* nodes, int* count)
{
    if(root->left){
        post_order_traversal(root->left, nodes, count);
    }
    if(root->right){
        post_order_traversal(root->right, nodes, count);
    }
    nodes[(*count)++] = root->val;
}

/**
 * 
 * @param root TreeNode类 the root of binary tree
 * @return int整型二维数组
 * @return int* returnSize 返回数组行数
 * @return int** returnColumnSizes 返回数组列数
 */
int** threeOrders(struct TreeNode* root, int* returnSize, int** returnColumnSizes ) {
    // write code here
    int **pp_result = NULL;
    if(root){
        int tree_size = 0;
        get_nodeNum(root, &tree_size);
        if(tree_size){
            int index = 0;
            *returnSize = 3;
            pp_result = (int**)malloc(sizeof(int*) * 3);
            int *pre_order_result = (int*)malloc(sizeof(int) * tree_size);
            pre_order_traversal(root, pre_order_result, &index);
            pp_result[0] = pre_order_result;
            
            index = 0;
            
            int* in_order_result = (int*)malloc(sizeof(int) * tree_size);
            in_order_traversal(root, in_order_result, &index);
            pp_result[1] = in_order_result;
            
            index = 0;
            
            int* post_order_result = (int*)malloc(sizeof(int) * tree_size);
            post_order_traversal(root, post_order_result, &index);
            pp_result[2] = post_order_result;
            
            int *columns = (int*)malloc(sizeof(int)*3);
            columns[0] = columns[1] = columns[2] = tree_size;
            *returnColumnSizes = columns;
        }
    }
    return pp_result;
}

运行结果:

运行时间:3ms

占用内存:364k

标签:count,int,中序,牛客,二叉树,nodes,root,order,result
From: https://www.cnblogs.com/blue-Suri/p/17342978.html

相关文章

  • 牛客网——数组中出现次数超过一半的数字
    title:牛客网——数组中出现次数超过一半的数字题目描述:数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出0。示例:输入[1,2,3,2,2,2......
  • 牛客小白月赛71 补题记录
    AB:略C:可以转化为比较对数,然后直接模拟即可(longdouble128位表示范围\(-1.2\times10^{-4932}~1.2\times10^{4932}\))代码如下:#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;//------------------------intmain(void){ ios::sync_with_stdi......
  • 1110 完全二叉树
    给定一个树,请你判断它是否是完全二叉树。输入格式第一行包含整数 N,表示树的结点个数。树的结点编号为 0∼N−1。接下来 N 行,每行对应一个结点,并给出该结点的左右子结点的编号,如果某个子结点不存在,则用 - 代替。输出格式如果是完全二叉树,则输出 YES 以及最后一个结点......
  • 1102 反转二叉树
    以下是来自 MaxHowell@twitter 的内容:谷歌:我们的百分之九十的工程师都使用你编写的软件,但是你连在白板上反转二叉树都做不到,还是滚吧。现在,请你证明你会反转二叉树。输入格式第一行包含一个整数 N,表示树的结点数量。所有结点编号从 0 到 N−1。接下来 N 行,每行对......
  • 数据结构 ---> 二叉树 -->堆之解析_01
    老友们好!本期将对堆之构建进行解说!相信,初学此章节的老友!!或多或少,对前一期的部分代码,有所困惑!说真的,堆的有些内容理解起来还是挺困难的!好了,废话不多讲!开始本期的解析之旅吧!希望大家都能有所收获!!下面,先看一组,上一期的图示:>那么该如何操作,才能取得前几名的数值呢?针对这点,部分老友......
  • SDUTOJ 2128 树结构练习——排序二叉树的中序遍历
    树结构练习——排序二叉树的中序遍历TimeLimit:1000MSMemorylimit:65536K题目描述在树结构中,有一种特殊的二叉树叫做排序二叉树,直观的理解就是——(1).每个节点中包含有一个关键值(2).任意一个节点的左子树(如果存在的话)的关键值小于该节点的关键值(3).任意一个节......
  • [牛客]链表分割
    编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前。牛客链接最简单思路因为头插必然改变顺序,所以使用尾插大于的尾插到一个链表小于的尾插到一个链表然后链接起来使用哨兵位的头结点,防止太多问题的产生/*structListNode{intval;struc......
  • 全二叉树
    全二叉树(CompleteBinaryTree)是一种特殊的二叉树,它的所有叶子节点都在同一层上,而且除了最后一层之外,其它层的节点数都达到了最大值,最后一层的节点从左到右依次排列。具有n个节点的完全二叉树,其节点编号从1到n,按照从上到下、从左到右的顺序进行编号,则有以下性质:如果一个节点的......
  • 牛客动态规划1选做
    [NOIP2002]过河卒题目链接进行记忆化搜索,然后强制把马所在的点和控制的点赋值为0#include<bits/stdc++.h>#defineintlonglongusingnamespacestd;set<pair<int,int>>st;intf[25][25];intcalc(intx,inty){if(x==0&&y==0)returnf[x][y]=1......
  • 浅谈全局平衡二叉树
    首先,这个是GBT,不是GPT。其次,那个是ChatGPT,不是ChatGBT。原理我们先考虑一个经典的问题:单点修树上最大权独立集问题,也就是LuoguP4719【模板】"动态DP"&动态树分治。使用树剖维护矩阵可以做到\(O(n\log^2n)\)的复杂度,可以通过\(10^5\)的数据。那我们能不能把这个问题优化......