二叉树的垂直顺序遍历-新测试用例

时间:2019-05-30 01:42:00

标签: tree-traversal

我需要执行二叉树的垂直顺序遍历。我编写了实现有序遍历的代码。它适用于大多数测试用例。 但是失败了。

struct TreeNode1
{
    int key;
    int pos;
    int height;
    TreeNode1(int k,int p, int h) : key(k), pos(p), height(h) {}
};
std::map<int, vector<TreeNode1>> vertical_tr;

void traverse(TreeNode* A,int pos,int height);

vector<vector<int> > Solution::verticalOrderTraversal(TreeNode* A) 
{
    //vec
    vector<vector<int>> result;
    result.clear();
    vertical_tr.clear();
    traverse(A,0,0);

    for (auto itr = vertical_tr.begin(); itr != vertical_tr.end(); ++itr) 
    { 
       vector<TreeNode1> temp1 ; temp1.clear();
       temp1 = itr->second;
       vector<int> temp2; temp2.clear();
       for(int i=0;i<temp1.size();i++)
       {
           temp2.push_back(temp1[i].key);
       }
       result.push_back(temp2);
    } 
    return result;
}
void traverse(TreeNode* A,int pos,int height)
{
   if(A==NULL) return;
  // vector < TreeNode1 > vec;
  // vec.push_back(TreeNode1(A->val,pos,height));
   if (vertical_tr.find(pos)!=vertical_tr.end())
   {
       //cout<<"here \n";
       auto itr = vertical_tr.find(pos);
       vector<TreeNode1> temp ; temp.clear();
       temp = itr->second;
       temp.push_back(TreeNode1(A->val,pos,height));
       /*for(int i=0;i< temp.size();i++)
        {
            cout<<temp[i]<< " : ";
        }
        cout<<"\n";*/

        std::sort(temp.begin(), temp.end(), 
        [] (const TreeNode1& struct1, const TreeNode1& struct2)
        {
            return (struct1.height < struct2.height);
        }
    );
    itr->second=temp;
        //sort(temp.begin(), temp.end(), compareHeight(ht));
   }
   else
   {
       vector<TreeNode1> temp;
       temp.clear();
       temp.push_back(TreeNode1(A->val,pos,height));
       vertical_tr[pos]=temp;
   }
   traverse(A->left,pos-1,height+1);
   //out<<"pos " << pos << "val" << A->val <<"\n";

   traverse(A->right,pos+1,height+1);
}

输入: 139 8958 4812 4370 6034 360 1572 -1 3487 1216 6218 1380 5258 -1 3672 8645 -1 2187 -1 1297 3176 -1 3587 2400 1220 7530 -1 2082 1443 9407 -1 292 1847 2077 -1 5338 9868 400 1 7072 2556 -1 8544 8970 -1 5152 -1 1775 4173 -1 882 3578 -1 974 -1 5034 9473 -1 591 4155 2372 5071 4575 200 2029 9440 180 5631 -1 3699 5880 3383 -1 2191 2635 -1 5388 5544 179 8450 2441 -1 254 7012 224 6142 6696 4063 -1 2673 2005 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1

实际结果:  [5071] [7072] [1220 4575 200] [3672 2556] [3487 7530 2029 9440 5631 3699 5544] [6034 8645 3587 8544 8970 5152 3578 5034] [4812 1216 6218 5258 9868 5338 1847 1443 1443 2082 4063 6142 7012 254 8450 179 3383 5880 180] [8958 360 1572 2187 1297 3176 2400 1775 974 9473 591 4155] [4370 1380 9407 292 2077 4001 2191 2441 224 6696 2673] [4173 882 2372] [2635 5388 2005]

预期结果:  [5071] [7072] [1220 4575 200] [3672 2556] [3487 7530 2029 9440 5631 3699 5544] [6034 8645 3587 8544 8970 5152 3578 5034] [4812 1216 6218 5258 2082 1443 1847 5338 9868 180 5880 3383 179 8450 254 7012 6142 4063] [8958 360 1572 2187 1297 3176 2400 1775 974 9473 591 4155] [4370 1380 9407 292 2077 4001 2191 2441 224 6696 2673] [4173 882 2372] [2635 5388 2005]

0 个答案:

没有答案