题目的意思:

输入很多个节点,包括路径和数值,但是不一定这些全部可以构成一棵树,问题就是判断所给的能否构成一棵树,且没有多余。

网上其他大神已经给出了题目意思:比如我一直很喜欢的小白菜又菜的博客

说一声: 

之前看了网上好多代码,看的大段大段的,看不下去了,就准备自己写了,结果写出来也是大段大段的,其实中间有不少是被注释掉的测试代码,可忽略不计!



// 测试样例:udebug :链接

// 我整个代码最核心的整体思想就是把全部节点放到map中,

// 然后层级遍历到的就放到vector(V)中,并从map中删除(重点),最后如果map有剩余就认为not complete!

<span style="font-family:Courier New;font-size:14px;">#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <map>
#include <queue>
using namespace std;
struct s{
    string path;   //节点路径
    int value;      //节点值
};
vector <int> v;     //存放可输出的树的节点
map <string, int> m;  //存放树
map <string, int> ::iterator it,it1,it2;
queue <s> q;
//这个地方是重点,就是如何层级输遍历输出;
//在网上看了一下,层级遍历可以用队列,对于每个在队列里面的pop之后,把他的左儿子和右儿子加到队列里(如果有)
void levelordertravel(){
    s temp;
    string t,t1,x,y;
    //如果连根都没有,直接返回了
    it=m.find("");
    if(it!=m.end()) q.push({it->first,it->second});
    else return;
    while(!q.empty()){
        temp=q.front();
        //cout<<temp<<endl;
        v.push_back(temp.value);
        t=temp.path;
        t1=t;
        m.erase(t);
        q.pop();
        //对于每个path,在map找加L和加R的
        x=t.append("L");
        it1=m.find(x);
        y=t1.append("R");
        it2=m.find(y);

        if(it1!=m.end()){
            q.push({it1->first,it1->second});
            //cout<<"left son is in"<<endl;
        }
        if(it2!=m.end()){
            q.push({it2->first,it2->second});
            //cout<<"rigth son is in"<<endl;
        }
    }
}
bool input(){
    string str;
    while(cin>>str){
        string path, value1;
        int value, p, length;
        if(str == "()") break;
        length = str.length();
        p = str.find(',');  //,的位置
        value1 = str.substr(1,p-1);
        value = atoi(value1.c_str());  //这个地方把string转换成int
        path = str.substr(p+1,length-1-(p+1));
        it=m.find(path);
        if(it!=m.end()) path="XXX";
        m[path] = value;
    }
    return !cin.eof();
}
int main()
{
    while(input()){   //这里的输入,没有循环输入的话就会wronganswer 就因为这个问题花费了很久时间
    //int len=m.size();
    //for(it=m.begin();it!=m.end();it++) cout<<it->first<<" "<<it->second<<endl;
    //cout<<endl;
    levelordertravel();
//    for(int i=0;i<v.size();i++){
//            cout<<v[i];
//            if(i!=v.size()-1) cout<<" ";
//        }
//        cout<<endl;
    if(m.size()!=0) cout<<"not complete"<<endl;
    if(m.size()==0){
        for(int i=0;i<v.size();i++){
            cout<<v[i];
            if(i!=v.size()-1) cout<<" ";
        }
        cout<<endl;
    }
    m.clear();
    v.clear();
    }
    return 0;
}</span>

最新文章

  1. IT从业者的职业道路(从程序员到部门经理) - 项目管理系列文章
  2. PHP基础OOP(二) 多态
  3. PYTHON--CLASS
  4. test_wifi
  5. 您应该了解的 Windows Azure 网站在线工具
  6. iOS开发 - Swift使用JavaScriptCore与JS交互
  7. 简易 HTTP Server 实现(JAVA)
  8. 简易js模板引擎
  9. Elasticsearch常用基础操作
  10. POJ 1741 Tree(树的点分治,入门题)
  11. 你应该学会的Python多版本管理工具Pyenv
  12. php json_encode与json_decode详解及实例
  13. 还能不能愉快地起一个web服务啦?——1st Step!白话http和代码交互的那点儿事儿~
  14. LeetCode题解之 Intersection of Two Arrays
  15. ubuntu18.04 安装mysql server
  16. PHP策略模式demo
  17. 14款优秀的JavaScript调试工具大盘点
  18. XSS payload 大全
  19. linux中nfs启动报rpcbind.socket failed to listen on sockets: Address family not supported by protocol
  20. spring-framework-reference(5.1.1.RELEASE)中文版——Core部分

热门文章

  1. 关于自己写C++的一点风格
  2. .NET 基础 一步步 一幕幕[面向对象之构造函数、析构函数]
  3. Security Policy:行级安全(Row-Level Security)
  4. ExtJS 4.2 组件介绍
  5. 使用etree.HTML的编码问题
  6. OpenCASCADE AIS Manipulator
  7. js学习之类型识别
  8. [原]分享一下我和MongoDB与Redis那些事
  9. 【干货分享】流程DEMO-事务呈批表
  10. H3 BPM让天下没有难用的流程之功能介绍