当前位置: 首页 > news >正文

二叉树的锯齿形层次遍历

问题描述

给定一个二叉树,返回其节点值的锯齿形层次遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。

例如:

给定二叉树 [3,9,20,null,null,15,7],

    3

   / \

  9  20

     /  \

   15   7

返回锯齿形层次遍历如下:

[

  [3],

  [20,9],

  [15,7]

]

程序输出:

3 20 9 15 7 

可使用以下main函数:

#include <iostream>

#include <queue>

#include <cstdlib>

#include <cstring>

using namespace std;

struct TreeNode

{

    int val;

    TreeNode *left;

    TreeNode *right;

    TreeNode() : val(0), left(NULL), right(NULL) {}

    TreeNode(int x) : val(x), left(NULL), right(NULL) {}

    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}

};

TreeNode* inputTree()

{

    int n,count=0;

    char item[100];

    cin>>n;

    if (n==0)

        return NULL;

    cin>>item;

    TreeNode* root = new TreeNode(atoi(item));

    count++;

    queue<TreeNode*> nodeQueue;

    nodeQueue.push(root);

    while (count<n)

    {

        TreeNode* node = nodeQueue.front();

        nodeQueue.pop();

        cin>>item;

        count++;

        if (strcmp(item,"null")!=0)

        {

            int leftNumber = atoi(item);

            node->left = new TreeNode(leftNumber);

            nodeQueue.push(node->left);

        }

        if (count==n)

            break;

        cin>>item;

        count++;

        if (strcmp(item,"null")!=0)

        {

            int rightNumber = atoi(item);

            node->right = new TreeNode(rightNumber);

            nodeQueue.push(node->right);

        }

    }

    return root;

}

int main()

{

    TreeNode* root;

    root=inputTree();

    vector<vector<int> > res=Solution().zigzagLevelOrder(root);

    for(int i=0; i<res.size(); i++)

    {

        vector<int> v=res[i];

        for(int j=0; j<v.size(); j++)

            cout<<v[j]<<" ";

    }

}

输入说明

首先输入结点的数目n(注意,这里的结点包括题中的null空结点)

然后输入n个结点的数据,需要填充为空的结点,输入null。

输出说明

输出结果,每个数据的后面跟一个空格。

输入范例

7
3 9 20 null null 15 7

输出范例

3 20 9 15 7 

实现思路

对树进行层次遍历,只需要多一步操作:用一个变量记录遍历的是哪一层的数据,若为偶数则要将遍历该层得到的一维数组进行reverse。

实现代码
#include <iostream>#include <queue>#include <cstdlib>#include <cstring>#include<algorithm>using namespace std;struct TreeNode{int val;TreeNode *left;TreeNode *right;TreeNode() : val(0), left(NULL), right(NULL) {}TreeNode(int x) : val(x), left(NULL), right(NULL) {}TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}};TreeNode* inputTree(){int n,count=0;char item[100];cin>>n;if (n==0)return NULL;cin>>item;TreeNode* root = new TreeNode(atoi(item));count++;queue<TreeNode*> nodeQueue;nodeQueue.push(root);while (count<n){TreeNode* node = nodeQueue.front();nodeQueue.pop();cin>>item;count++;if (strcmp(item,"null")!=0){int leftNumber = atoi(item);node->left = new TreeNode(leftNumber);nodeQueue.push(node->left);}if (count==n)break;cin>>item;count++;if (strcmp(item,"null")!=0){int rightNumber = atoi(item);node->right = new TreeNode(rightNumber);nodeQueue.push(node->right);}}return root;}class Solution{
public:vector<vector<int>> zigzagLevelOrder(TreeNode *root){vector<vector<int>> res;queue<TreeNode *>q;int i = 0;//记录是哪一层,若为偶数层则将该层数据reverseif(root!=NULL)q.push(root);while(!q.empty()){i++;int len = q.size();vector<int>tem;while(len){TreeNode *p = q.front();q.pop();tem.push_back(p->val);if(p->left) q.push(p->left);if(p->right) q.push(p->right);len--;}if(i%2==0){reverse(tem.begin(),tem.end());}res.push_back(tem);}return res;}
};int main(){TreeNode* root;root=inputTree();vector<vector<int> > res=Solution().zigzagLevelOrder(root);for(int i=0; i<res.size(); i++){vector<int> v=res[i];for(int j=0; j<v.size(); j++)cout<<v[j]<<" ";}}

http://www.xdnf.cn/news/1238437.html

相关文章:

  • 思途JSP学习 0802(项目完整流程)
  • day 44 文件的规范书写与拆分
  • 《 ThreadLocal 工作机制深度解析:高并发场景的利与弊》
  • Spring+K8s+AI实战:3全栈开发指南
  • Redis实战(7)-- 高级特性 Redis Stream数据结构与基础命令
  • HCIE-Datacom题库_07_设备【道题】
  • kafka与其他消息队列(如 RabbitMQ, ActiveMQ)相比,有什么优缺点?
  • Qt-vs加载exe图标
  • 日常--详细介绍qt Designer常用快捷键(详细图文)
  • 其它IO函数
  • Fay数字人如何使用GPT-SOVITS进行TTS转换以及遇到的一些问题
  • 《基于通道注意力与空洞卷积的胸片肺气肿检测算法》论文解析
  • [硬件电路-138]:模拟电路 - 什么是正电源?什么是负电源?集成运放为什么有VCC+和VCC-
  • Python切片命名技术详解:提升代码可读性与维护性的专业实践
  • 2106. 摘水果
  • 关于assert()函数,eval()函数,include
  • 第N个泰波那契数
  • Spring lookup-method实现原理深度解析
  • e2studio开发RA4M2(6)----GPIO外部中断(IRQ)配置
  • 信创及一次ORACLE到OB的信创迁移
  • 图像、视频、音频多模态大模型中长上下文token压缩方法综述
  • 使用 Vuepress + GitHub Pages 搭建项目文档
  • 【Bluetooth】【Transport层篇】第四章 基于基础UART的蓝牙硬件发送协议 UART H4 Transport详解
  • Docker 国内可用镜像
  • 关于 xrdp远程桌面报错“Error connecting to sesman on 127.0.0.1:3350“的解决方法
  • [自动化Adapt] 录制引擎
  • 计算机视觉CS231n学习(2)
  • 第六章第三节 TIM 输出比较
  • Java 大视界 -- Java 大数据在智能教育学习资源个性化推荐与学习路径动态调整中的深度应用(378)
  • ARPO:让LLM智能体更高效探索