PAT:03-2. List Leaves,Go语言解答

先上题目,大概意思是给出一颗二叉树,输出叶子节点,输出的顺序是从上到下,从左往右。输入的数据,第一行表示节点个数,后面的行表示节点的数据,例如"1 -"表示序号为0的节点的左子节点为1,没有右子节点。

-----------------------------------------------------------------------------------------------------------

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

Input Specification:

Each input file contains one test case. For each case, the firstline gives a positive integer N (<=10) which is the total numberof nodes in the tree -- and hence the nodes are numbered from 0 toN-1. Then N lines follow, each corresponds to a node, and gives theindices of the left and right children of the node. If the childdoes not exist, a "-" will be put at the position. Any pair ofchildren are separated by a space.

Output Specification:

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

Sample Input:
8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6
Sample Output:
4 1 5
-------------------------------------------------------------------------------

实现的思路

1.首先要输入二叉树,按照输入的二叉树数量大小,建立二叉树数组,数组的序号默认为节点的序号

2.找到root节点

3.采用层序遍历的方式遍历,遍历时如果发现该节点是叶子节点就输出,需要用list实现队列的功能

-------------------------------------------------------------------------------------------------------------

  1. package main  
  2.   
  3. import(  
  4.     "fmt"  
  5.     "container/list"  
  6.  
  7. func main(){  
  8.     var int  
  9.     _, err := fmt.Scanf("%d\n",&N)  
  10.     if err != nil  
  11.         fmt.Println("error", err)  
  12.      
  13.     nodes := make([]treeNode,N)  
  14.     hasNode := make([]bool,N)  
  15.       
  16.     var left,right int32  
  17.     for := 0; i
  18.         nodes[i].data i//赋值  
  19.         _, err := fmt.Scanf("%c %c\n",&left,&right)//输入i号节点的左右子节点  
  20.         if err != nil  
  21.             fmt.Println("error", err)  
  22.          
  23.         if left != '-'{  
  24.             left left '0'  
  25.             nodes[i].left &nodes[left]  
  26.             hasNode[left] true//表示该节点的引用存在  
  27.          
  28.         if right != '-'{  
  29.             right right '0'  
  30.             nodes[i].right &nodes[right]  
  31.             hasNode[right] true//表示该节点的引用存在  
  32.          
  33.      
  34.       
  35.     //查询根节点  
  36.     var root int   
  37.     for i,flag := range hasNode{  
  38.         if(flag == false){  
  39.             root i//找到根节点  
  40.          
  41.      
  42.       
  43.     ls := list.New()  
  44.     ls.PushBack(nodes[root])//压入root  
  45.     ele := ls.Front();   
  46.     for first := true ele != nil;{//层序遍历  
  47.         node := (ele.Value).(treeNode)//断言为treeNode类型,node为一棵树  
  48.         if node.left != nil{  
  49.             ls.PushBack(*node.left)//node.left是指向treeNode的指针,加*解引用  
  50.          
  51.         if node.right != nil  
  52.             ls.PushBack(*node.right)  
  53.                 
  54.         //检查是否为叶子节点  
  55.         if node.left == nil && node.right == nil{  
  56.             if first{//如果是第一个输出的数据  
  57.                 fmt.Print(node.data)  
  58.                 first false//第一次已经没有了  
  59.             }else{  
  60.                 fmt.Print(" ",node.data)  
  61.                         
  62.          
  63.         ls.Remove(ele)//干掉它  
  64.         ele ls.Front();   
  65.           
  66.      
  67.  
  68.   
  69. type treeNode struct{  
  70.     data int  
  71.     left *treeNode  
  72.     right *treeNode  
  73.  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值