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

二叉树的层次遍历 II

问题描述

给定一个二叉树,返回其节点值自底向上的层次遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)

例如:

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

    3

   / \

  9  20

     /  \

   15   7

返回其自底向上的层次遍历为:

[

  [15,7],

  [9,20],

  [3]

]

程序输出为:

15 7 9 20 3

可使用以下main函数:

#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;

}

int main()

{

    TreeNode* root;

    root=inputTree();

    vector<vector<int> > res=Solution().levelOrderBottom(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

输出范例

15 7 9 20 3 

实现思路

bilibili代码随想录:二叉树 5. 层序遍历

最后将得到的res数组进行一个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>> levelOrderBottom(TreeNode *root){vector<vector<int>> res;queue<TreeNode*> q;if(root) q.push(root);while(!q.empty()){int len = q.size();vector<int>tem;while(len){TreeNode *p = q.front();tem.push_back(p->val);q.pop();len--;if(p->left) q.push(p->left);if(p->right) q.push(p->right);}res.push_back(tem);}reverse(res.begin(),res.end());return res;}};int main(){TreeNode* root;root=inputTree();vector<vector<int> > res=Solution().levelOrderBottom(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/311570.html

相关文章:

  • 算法: 字符串part02: 151.翻转字符串里的单词 + 右旋字符串 + KMP算法28. 实现 strStr()
  • Redis数据库存储键值对的底层原理
  • 信创应用服务器TongWeb安装教程、前后端分离应用部署全流程
  • Web API安全防护全攻略:防刷、防爬与防泄漏实战方案
  • Dispersive Loss:为生成模型引入表示学习 | 如何分析kaiming新提出的dispersive loss,对扩散模型和aigc会带来什么影响?
  • 二、无摩擦刚体捉取——抗力旋量捉取
  • uniapp 数组的用法
  • 【c#窗体荔枝计算乘法,两数相乘】2022-10-6
  • Python Pandas.from_dummies函数解析与实战教程
  • 【语音技术】什么是动态实体
  • 【解决错误】IDEA启动SpringBoot项目 出现:Command line is too long
  • 5734 孤星
  • process_vm_readv/process_vm_writev 接口详解
  • 如何在 Ubuntu 24.04 或 22.04 LTS Linux 上安装 Guake 终端应用程序
  • Next.js 怎么使用 Chakra UI
  • LINUX82 shell脚本变量分类;系统变量;变量赋值;四则运算;shell
  • 落霞归雁·思维框架
  • 队列的使用【C++】
  • 【王阳明代数讲义】基本名词解释
  • InfluxDB 与 Node.js 框架:Express 集成方案(一)
  • 【RK3568 RTC 驱动开发详解】
  • 操作系统-lecture5(线程)
  • Terraria 服务端部署(Docker)
  • Trae + Notion MCP:将你的Notion数据库升级为智能对话机器人
  • 自动驾驶中的传感器技术14——Camera(5)
  • C#开发入门指南_学习笔记
  • Clickhouse#表记录转换为insert语句
  • 回归预测 | Matlab实现CNN-LSTM-Multihead-Attention多变量回归预测
  • Spring AI MCP 技术深度解析:从工具集成到企业级实战
  • PyQt6教程(003):运行QTDesigner生成的UI文件