03-树2 List Leaves (25 分)

03-树2 List Leaves (25 分)

Given a tree, you are supposed to list all the leaves in the order of top down, and left to right.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (10) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N1. Then N lines follow, each corresponds to a node, and gives the indices of the left and right children of the node. If the child does not exist, a "-" will be put at the position. Any pair of children are separated by a space.

Output Specification:

For each test case, print in one line all the leaves' indices in the order of top down, and left to right. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.

Sample Input:

8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6

Sample Output:

4 1 5


代码里应该有些是没用的.需要你自己删除掉.


using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Net;
using System.Text;
using System.Xml;

struct Point
{
    public string C;
    public string X;
    public string Y;
    public Point(string v1, string v2, string v3) : this()
    {
        this.C = v1;
        this.X = v2;
        this.Y = v3;
    }
}

class P
{


    public XmlDocument doc = new XmlDocument();
    public P(string v2) : this(int.Parse(v2))
    {

    }
    List<Point> tempList = new List<Point>();
    public P(int v1)
    {
        if (v1 > 0)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < v1; i++)
            {
                var temp = Console.ReadLine().Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);


                tempList.Add(new Point(i.ToString(), temp[0], temp[1]));
                sb.Append(temp[0]);
                sb.Append(temp[1]);
            }
            var item = new Point();
            string sss = sb.ToString();
            for (int i = 0; i < v1; i++)
            {
                if (!sss.Contains(i + ""))
                {
                    item = tempList[i];
                }
            }



            var xNode = doc.CreateNode(XmlNodeType.Element, "item", "");

            var attr1 = doc.CreateAttribute("x");
            attr1.Value = item.X;
            var attr2 = doc.CreateAttribute("y");
            attr2.Value = item.Y;
            var attr3 = doc.CreateAttribute("n");
            attr3.Value = item.C;

            xNode.Attributes.Append(attr1);
            xNode.Attributes.Append(attr2);
            xNode.Attributes.Append(attr3);
            GetNodes(xNode);

            doc.AppendChild(xNode);

        }
    }

    private void GetNodes(XmlNode xNode)
    {
        int x = -1;
        int y = -1;
        if (int.TryParse(xNode.Attributes["x"].Value, out x))
        {

            var item = tempList[x];

            var node = doc.CreateNode(XmlNodeType.Element, "item", "");
            var attr1 = doc.CreateAttribute("x");
            attr1.Value = item.X;
            var attr2 = doc.CreateAttribute("y");
            attr2.Value = item.Y;
            var attr3 = doc.CreateAttribute("n");
            attr3.Value = item.C;

            node.Attributes.Append(attr1);
            node.Attributes.Append(attr2);
            node.Attributes.Append(attr3);
            GetNodes(node);

            xNode.AppendChild(node);
        }
        if (int.TryParse(xNode.Attributes["y"].Value, out y))
        {

            var item = tempList[y];

            var node = doc.CreateNode(XmlNodeType.Element, "item", "");
            var attr1 = doc.CreateAttribute("x");
            attr1.Value = item.X;
            var attr2 = doc.CreateAttribute("y");
            attr2.Value = item.Y;
            var attr3 = doc.CreateAttribute("n");
            attr3.Value = item.C;

            node.Attributes.Append(attr1);
            node.Attributes.Append(attr2);
            node.Attributes.Append(attr3);
            GetNodes(node);

            xNode.AppendChild(node);
        }
    }


    List<XmlNode> xnList = new List<XmlNode>();
  public string 获取子叶上为先(XmlNode node)
    {
        xnList.Add(node);
        string s="";
        for (int i=0;i< xnList.Count;i++)
        {
            if(xnList[i].HasChildNodes)
            {
                foreach (XmlNode item in xnList[i].ChildNodes)
                {
                    xnList.Add(item);
                }
            }
            else
            {
                s += xnList[i].Attributes["n"].Value+" ";
            }
        }

        return s;

    }
  public string 获取子叶左为先(XmlNode node)
    {
        string s = "";

        if(node.HasChildNodes)
        {
            foreach (XmlNode item in node.ChildNodes)
            {
                s += 获取子叶左为先(item);
            }
            

        }else
        {
            s += node.Attributes["n"].Value+" ";
        }
        


        return s;

    }
}

class T
{


    static bool 对比(XmlNode p1, XmlNode p2)
    {   if (p1.Attributes["n"] .Value== p2.Attributes["n"].Value)
        {

            if (p1.ChildNodes.Count == 0 &&
                        p2.ChildNodes.Count == 0)
            {

                return true;
            }
            if (p1.ChildNodes.Count == 1 &&
                p2.ChildNodes.Count == 1)
            {
                return 对比(p1.ChildNodes[0], p2.ChildNodes[0]);
            }
            if (p1.ChildNodes.Count == 2 &&
                      p2.ChildNodes.Count == 2)
            {
                var b00 = 对比(p1.ChildNodes[0], p2.ChildNodes[0]);
                var b11 = 对比(p1.ChildNodes[1], p2.ChildNodes[1]);
                var b10 = 对比(p1.ChildNodes[1], p2.ChildNodes[0]);
                var b01 = 对比(p1.ChildNodes[0], p2.ChildNodes[1]);
                return (b00 && b11) || (b10 && b01);
            }
        }
        return false;
    }
    static void Main(string[] args)
    {
        P p1 = new P(Console.ReadLine());


        var s=p1.获取子叶上为先(p1.doc.FirstChild);
        Console.WriteLine(s.Trim());

        return;



        P p2 = new P(Console.ReadLine());



        if (p1.doc.ChildNodes.Count == 0 &&
            p2.doc.ChildNodes.Count == 0)
        {
            Console.WriteLine("Yes");
            return;
        }
        else
        {
            bool b = 对比(p1.doc.ChildNodes[0], p2.doc.ChildNodes[0]);


            if (b)
                Console.WriteLine("Yes");
            else
                Console.WriteLine("No");
        }
    }


}
 

猜你喜欢

转载自www.cnblogs.com/interim/p/9723206.html
今日推荐