python基础编程——树的高度

树的高度

题目描述
现在有一棵合法的二叉树,树的节点都是用数字表示,现在给定这棵树上所有的父子关系,求这棵树的高度
输入描述:

输入的第一行表示节点的个数n(1 ≤ n ≤ 1000,节点的编号为0到n-1)组成,
下面是n-1行,每行有两个整数,第一个数表示父节点的编号,第二个数表示子节点的编号

输出描述:

输出树的高度,为一个整数

示例:
输入:
5
0 1
0 2
1 3
1 4

输出:
3

mycode:
通过率为50%

cnt = int(input())
Btree = [0] * (cnt + 1)

for i in range(0, cnt-1):
    x, y = map(int, input().split(" "))
    Btree[y] = x

height = 1
for j in range(cnt-1, 0, -1):
    index = j
    count = 1
    while index:
        index = Btree[index]
        count += 1
    if count > height:
        height = count

print(height)

other’s code:
通过率为100%

n = input()
tree = [1] * n
childnum = [0] * n
for i in range(0, n-1):
    parent, this = map(int, input().split(" "))
    if childnum[parent] >= 2:
        tree[this] = 0
        childnum[this] = 2  
        continue
    tree[this] += tree[parent]
    childnum[parent] += 1
 
print (max(tree))

小结:
测试样例中有多叉树的情况,估计出题人的意图就是让我们将数据删减为二叉树。

补充:
本题中没有用到标准的树的insert来构建一个数。下面简单介绍:

树的列表表示:

def BinaryTree(r):
    return [r, [], []]
myTree = ['a',   #root     

           ['b',  #left subtree     
           ['d', [], []],    
           ['e', [], []] ],  
              
           ['c',  #right subtree      
           ['f', [], []],       [] ]       ]
def insertLeft(root,newBranch):   
    t = root.pop(1)   
    if len(t) > 1:       
        root.insert(1,[newBranch,t,[]])    
  else:        
        root.insert(1,[newBranch, [], []])   
        return root

def insertRight(root,newBranch):   
    t = root.pop(2)    
    if len(t) > 1:        
        root.insert(2,[newBranch,[],t])    
  else:        
        root.insert(2,[newBranch,[],[]])    
    	return root
 
def getRootVal(root):    
    return root[0] 
def setRootVal(root,newVal):    
    root[0] = newVal 
def getLeftChild(root):    
    return root[1] 
def getRightChild(root):    
    return root[2]

树的节点表示:

class BinaryTree:    
    def __init__(self,rootObj):        
        self.key = rootObj        
        self.leftChild = None        
        self.rightChild = None 
        
    def insertLeft(self,newNode):        
        if self.leftChild == None:            
            self.leftChild = BinaryTree(newNode)        
        else:            
            t = BinaryTree(newNode)            
            t.leftChild = self.leftChild            
            self.leftChild = t     
            
    def insertRight(self,newNode):        
        if self.rightChild == None:            
            self.rightChild = BinaryTree(newNode)       
        else:            
            t = BinaryTree(newNode)            
            t.rightChild = self.rightChild            
            self.rightChild = t      
    
    def getRightChild(self):        
        return self.rightChild   
    
    def getLeftChild(self):        
        return self.leftChild     
    
    def setRootVal(self,obj):        
        self.key = obj     
        
    def getRootVal(self):        
        return self.key  
    
r = BinaryTree('a')
print(r.getRootVal())
print(r.getLeftChild())
r.insertLeft('b')
print(r.getLeftChild())
print(r.getLeftChild().getRootVal())
r.insertRight('c')
print(r.getRightChild())
print(r.getRightChild().getRootVal())
r.getRightChild().setRootVal('hello')
print(r.getRightChild().getRootVal())
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值