ProDeveloperTutorial.com

教程和编程解决方案
菜单
  • Shell脚本
  • 系统设计
  • Linux系统编程
  • 4g LTE
  • 编码问题
  • C
  • C ++
  • DSA
  • GIT

通过使用队列显示反向级别订单遍历

前开发者教程 2020年10月8日

题:

给定一个二叉树根节点,以反向级别遍历显示所有节点。

例:

考虑下面给出的图像:

二元树

如上图所示,反向级别遍历为:7,15,18,30,10,25,16

解:

解决方案类似于级别顺序遍历,但有一个区别。

在这里,我们应该使用堆栈和队列。

这里的想法是进行级别顺序遍历,同时使元素出队,将这些元素推入堆栈。

将元素推入堆栈时,先推右孩子,然后推左孩子。

例:

1.如果我们只有一个节点,如下所示:

根节点不’t具有左或右节点。因此,从队列中删除并放入堆栈。由于树为空,因此从堆栈中删除并显示。

2.如果我们有2个节点,如下所示:

二元树

首先将根节点插入队列。
从队列中弹出并插入堆栈。

队列:空
堆叠:16

接下来转到合适的孩子,并插入队列

队:25
堆叠:16

接下来转到左孩子,并插入队列

队列:25,10
堆叠:16

现在,弹出两个元素并插入堆栈

列:NULL
堆叠:16,10,25

由于树为NULL,因此显示堆栈内容。

 

C ++解决方案

#include <iostream>
#include <queue>
#include <stack>
using namespace std;

// structure to hold binary tree node
struct Node
{
    int data;
    Node *left, *right;

    Node(int data)
    {
        this->data = data;
        this->left = this->right = nullptr;
    }
};


void display_reverse_level_order(Node *root) 
{ 
    stack <Node *> my_stack; 
    queue <Node *> my_queue; 
    my_queue.push(root); 
  

    while (my_queue.empty() == false) 
    { 
        // Dequeue node and make it root
        root = my_queue.front(); 
        my_queue.pop(); 
        my_stack.push(root); 
  
        // Enqueue right child 
        if (root->right) 
            my_queue.push(root->right);
  
        // Enqueue left child 
        if (root->left) 
            my_queue.push(root->left); 
    } 
  
    // display all the elements in stack 
    while (my_stack.empty() == false) 
    { 
        root = my_stack.top(); 
        cout << root->data << " "; 
        my_stack.pop(); 
    } 
} 

int main()  
{  
    Node* root = nullptr;
    /* Binary tree:
              16
            /   \
           /     \
          10      25
         /  \    /  \
        /    \  /    \
       7    15 18     30

    */

    root = new Node(16);
    root->left = new Node(10);
    root->right = new Node(25);
    root->left->left = new Node(7);
    root->left->right = new Node(15);
    root->right->left = new Node(18);
    root->right->right = new Node(30);

    int level = 2;

    cout<<"Solution = " <<endl;
    display_reverse_level_order(root); 

    return 0;  
}

 

输出:

Solution =
7 15 18 30 10 25 16
分享
电子邮件
鸣叫
领英
Reddit
绊倒
Pinterest的
上一篇文章
下一篇

关于作者

前开发者教程

每天我们都会讨论竞争性编程问题,请加入我们的网站:   电报频道

ProDeveloperTutorial.com

教程和编程解决方案
版权© 2020 ProDeveloperTutorial.com
从以下课程获得热门课程: 教育性的



  1. <center id="IGDpxhJ" class="I8OngXg"><hgroup id="qdRKCHt"></hgroup></center>
    <bdi id="zkinj0i" class="zEFVSJ7"><strike id="PzcRanY" class="PrlpwCA"><acronym id="y7BDLEQ" class="yg9NPxt"></acronym></strike></bdi>
    1. <audio id="WEzZxah"><progress id="A5iV8ha"><optgroup class="BcP6Rll"></optgroup></progress></audio>

      <footer id="OUnooU0" class="OfAmSQB"><blockquote id="SKPiz1M"></blockquote></footer>
      <source id="HbuOuTl" class="Htc2UQL"><option class="uAIUaIR"><audio id="LfnyrUr"></audio></option></source>


      • <q id="TOaR8Wz"><dt id="Bo0oPpz" class="B43TuJK"><wbr id="QEhKCe9"><applet class="yyPwsxE"></applet></wbr></dt></q><details id="y8vGcYX"><select id="t2A4dXS"></select></details>