当前位置: 首页 > 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.dtcms.com/a/312297.html

相关文章:

  • 日语学习-日语知识点小记-进阶-JLPT-真题训练-N1阶段(2):018年7月-JLPT-N1
  • TI 毫米波雷达开发:(四)毫米波雷达板开发所需软件
  • 记一次v-if和key错误使用,导致vue2的内存爆炸修复!
  • 著作权登记遇难题:创作者如何突破确权困境?
  • 【论文阅读|V2M: VISUAL 2-DIMENSIONAL MAMBA FOR IMAGE REPRESENTATION LEARNING】
  • 字节-面试
  • 性能测试工具ApacheBench、Jmeter
  • gitee使用教程
  • 昇思学习营-开发版-模型推理和性能优化
  • 在 Elasticsearch 中使用 LTR 参与检索
  • k8s+isulad 国产化技术栈云原生技术栈搭建3-master节点安装
  • 查找位置函数
  • 垃圾收集器G1ZGC详解
  • 2025年渗透测试面试题总结-2025年HW(护网面试) 77-1(题目+回答)
  • 2025年渗透测试面试题总结-2025年HW(护网面试) 76-1(题目+回答)
  • set和map使用
  • 技巧|SwanLab记录混淆矩阵攻略
  • express-jwt报错:Error: algorithms should be set
  • 【智能体cooragent】不同的单智能体调用的大模型的推理的输入与输出
  • 笔试——Day26
  • 【LLM】如何在Cursor中调用Dify工作流
  • Makefile 从入门到精通:自动化构建的艺术
  • 【Java基础知识 16】 数组详解
  • 微积分思想的严密性转变 | 极限、逼近与程序化
  • 计算机技术与软件专业技术资格(水平)考试简介
  • 【Pytorch✨】LSTM01 入门
  • 集成电路学习:什么是HAL硬件抽象层
  • 【设计模式】 3.设计模式基本原则
  • 对于考研数学的理解
  • 【攻防实战】记一次DOUBLETROUBLE攻防实战