zoukankan      html  css  js  c++  java
  • 【树论】二叉树的序遍历

    原题传送门

    思路


    此题是本蒟蒻迈向黄金段位的最后一道题,然而本蒟蒻只用了10min便搞⛏掉了它QAQ,看来自己还是有进步的!!!

    TIPS

    前序遍历:根-左-右
    中序遍历:左-根-右
    后序遍历:左-右-根
    然后大爆搜就ojbk啦~~~

    Code


    #include<iostream>
    #include<cstdio>
    #include<string>
    #include<vector>
    #include<algorithm>
    #include<cstdlib>
    #include<cmath>
    #include<stack>
    #include<map>
    using namespace std;
    
    struct ints
    {
    	int l;
    	int r;
    	ints(){l=r=0;}
    	ints(int a,int b){l=a;r=b;}
    };
    map<int,ints> gmap;
    int n,i,gleft,gright,maxdepth,maxwidth;
    int wide[10];
    
    void dfs1(int no)
    {
    	if(no==0)
    		return;
    	cout<<no<<" "; 
    	dfs1(gmap[no].l);
    	dfs1(gmap[no].r);
    }
    
    void dfs2(int no)
    {
    	if(no==0)
    		return;
    	dfs2(gmap[no].l);
    	cout<<no<<" "; 
    	dfs2(gmap[no].r);
    }
    
    void dfs3(int no)
    {
    	if(no==0)
    		return;
    	dfs3(gmap[no].l);
    	dfs3(gmap[no].r);
    	cout<<no<<" "; 
    }
    
    int main()
    {
        cin>>n;
        for(int i=1;i<=n;i++)
        {
        	cin>>gleft>>gright;
        	gmap[i]=ints(gleft,gright);
    	}
    	dfs1(1);
    	cout<<endl;
    	dfs2(1);
    	cout<<endl;
    	dfs3(1);
    	cout<<endl;
        return 0;
    }
    
  • 相关阅读:
    CQL
    gossip协议
    Cassandra删除数据的坑
    Cassandra维护数据一致性的策略
    Cassandra查询出错
    string的 insert
    数字游戏(数学推理
    deque
    暗黑字符串(递推
    最大的奇约数和
  • 原文地址:https://www.cnblogs.com/gongdakai/p/11285436.html
Copyright © 2011-2022 走看看