【问题标题】:Vertical Order Traversal using Iterative method使用迭代方法的垂直顺序遍历
【发布时间】:2021-11-30 04:25:45
【问题描述】:

我正在尝试使用映射和队列解决二叉树的垂直顺序遍历问题。我确实使用递归方式解决了它,但使用迭代方式我没有得到相同的答案。

       10
     /    \
    7      4
  /  \    /  \
 3   11  14   6

方法:

首先我声明了一个整数,用于存储与根节点的水平距离。

水平距离意味着离根的左边部分将被认为是-1,-2等等,就像一个负X轴,根是原点,从0开始。所以节点7将被赋予-1 , 3 将被赋予 -2。但是,10、11、14 将被赋予 0,因为它们不远离根节点而是位于相同的位置。向右,距离将变为正数,因此 4 将获得距离 1,6 将获得 2,依此类推。

首先,我将根推入队列并将水平距离更新为0。之后,我在地图中添加水平距离作为键和根数据作为值。最后,我从队列中弹出根并检查它的左右孩子,如果左右孩子可用,我分别将左右孩子推入队列并相应地更新水平距离。然后我对完整的二叉树进行了相同的过程。

然后,我只是遍历了地图的第二部分以获得答案。

The Answer should be :
3 
7 
10 11 14 
4 
6 

The Answer I received is : 
10 7 4 3 11 14 6 

这是我的代码:

#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

struct Node
{
    int data;
    Node *left;
    Node *right;

    Node(int val)
    {
        data = val;
        left = NULL;
        right = NULL;
    }
};

map<int, vector<int>> verticalPrint(Node *root)
{
    queue<Node *> qi;
    map<int, vector<int>> mp;

    int Hd = 0;
    qi.push(root);

    while (!qi.empty())
    {
        Node *temp = qi.front();
        mp[Hd].push_back(temp->data);
        qi.pop();

        if (temp->left != NULL)
        {
            qi.push(temp->left);
            Hd -= 1;
        }
        if (temp->right != NULL)
        {
            qi.push(temp->right);
            Hd += 1;
        }
    }
    return mp;
}

int main()
{
    Node *root = new Node(10);
    root->left = new Node(7);
    root->right = new Node(4);
    root->left->left = new Node(3);
    root->left->right = new Node(11);
    root->right->left = new Node(14);
    root->right->right = new Node(6);

    map<int, vector<int>> mp = verticalPrint(root);

    map<int, vector<int>>::iterator it;

    for (it = mp.begin(); it != mp.end(); it++)
    {
        for (int i = 0; i < it->second.size(); i++)
        {
            cout << it->second[i] << " ";
        }
        cout << endl;
    }

    return 0;
}

【问题讨论】:

    标签: c++ vector queue maps binary-tree


    【解决方案1】:

    您不能像这样使用单个 Hd 变量。请注意,在第一次迭代中,Hd 将变为 -1 并返回 0,因为根同时具有左右孩子。因此,在下一次迭代中,您再次从 0 开始,但您从队列中拉出的节点与 Hd 的值无关。

    相反,将pairs 放入队列中:节点及其对应水平距离的组合。然后它会正常工作:

    map<int, vector<int>> verticalPrint(Node *root)
    {
        queue<pair<int, Node *>> qi;
        map<int, vector<int>> mp;
    
        qi.push({0, root});
    
        while (!qi.empty())
        {
            pair<int, Node*> item = qi.front();
            int hd = item.first;
            Node * temp = item.second;
            mp[hd].push_back(temp->data);
            qi.pop();
    
            if (temp->left != NULL)
            {
                qi.push({hd - 1, temp->left});
            }
            if (temp->right != NULL)
            {
                qi.push({ hd+1, temp->right});
            }
        }
        return mp;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-13
      • 2014-05-10
      • 2021-05-17
      • 2021-02-16
      • 1970-01-01
      • 2011-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多