数据结构:List Leaves——Swift

List Leaves

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

 

代码:

import Foundation

struct TreeNode {
    var Left: Int?
    var Right: Int?
    init() {
        self.Left = nil
        self.Right = nil
    }
}

var T1 = [TreeNode](repeating: TreeNode.init(), count: 10)
var Arr = [Int]()
let null = -1
var count = 0

func Creat(Tree T: inout [TreeNode]) -> Int{
    let n = readLine()
    let c = Int(n!)!
    guard c != 0 else {
        return null
    }
    var root = 0
    var left: String
    var right: String
    for i in 0...c-1 {
        let word = readLine()!.components(separatedBy: CharacterSet.whitespaces)
        left = word[0]
        right = word[1]
        if left == "-"{
            T[i].Left = null
        }else{
            T[i].Left = Int(left)
            root -= T[i].Left!
        }
        if right == "-"{
            T[i].Right = null
        }else{
            T[i].Right = Int(right)
            root -= T[i].Right!
        }
        root += i
    }
    return root
}

func ShowLeaves(Root R: Int){
    guard R != null else {
        return
    }
    var t = null
    if T1[R].Left != null && T1[R].Right != null {
        Arr.remove(at: Arr.index(0, offsetBy: count))
        Arr.append(T1[R].Left!)
        Arr.append(T1[R].Right!)
        t = Arr[Arr.index(0, offsetBy: count)]
    }else if T1[R].Left != null {
        Arr.remove(at: Arr.index(0, offsetBy: count))
        Arr.append(T1[R].Left!)
        t = Arr[Arr.index(0, offsetBy: count)]
    }else if T1[R].Right != null {
        Arr.remove(at: Arr.index(0, offsetBy: count))
        Arr.append(T1[R].Right!)
        t = Arr[Arr.index(0, offsetBy: count)]
    }else if T1[R].Left == null && T1[R].Right == null{
        count += 1
        if count <= Arr.count-1{
            t = Arr[Arr.index(0, offsetBy: count)]
        }else{
            return
        }
    }
    ShowLeaves(Root: t)
}

let R = Creat(Tree: &T1)
if R != null {
    Arr.append(R)
}
ShowLeaves(Root: R)
if Arr.isEmpty{
    print("-",terminator: "")
}else if Arr.count < 2 {
    print(Arr[0], terminator: "")
}else{
    for i in 0...Arr.count - 2{
        print(Arr[i], terminator: " ")
    }
    print(Arr.last!,terminator: "")
}

 结果:

 

 

 结语:以上为Swift编写的List Leaves算法。

posted @ 2021-08-02 16:28  bokeyuan0029  阅读(65)  评论(0)    收藏  举报