Using self.xxxx as a default parameter - Python

前端 未结 3 705
失恋的感觉
失恋的感觉 2020-11-27 19:54

I\'m trying to simplify one of my homework problems and make the code a little better. What I\'m working with is a binary search tree. Right now I have a function in my

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-27 20:18

    larsmans answered your first question

    For your second question, can you simply look before you leap to avoid recursion?

    def makeList(self, aNode=None):
        if aNode is None:
            aNode = self.root
        treeaslist = [aNode.data]
        if aNode.lChild:
            treeaslist.extend(self.makeList(aNode.lChild))
        if aNode.rChild:
            treeaslist.extend(self.makeList(aNode.rChild))
        return treeaslist
    

提交回复
热议问题