二叉树

二叉树的遍历:

class Tree():
    def __init__(self, val):
        self.val = val
        self.left, self.right = None, None

root = Tree(2)
root.right = Tree(10)
root.right.left = Tree(20)
root.left = Tree(5)
root.left.right = Tree(19)
root.left.left = Tree(9)

前序遍历:根节点,左节点,右节点
class BeforeTravel():
    def __init__(self):
        self.list = []

    def Before(self, root):
        if root:
            self.list.append(root.val)
            self.Before(root.left)
            self.Before(root.right)
        return self.list
res = BeforeTravel().Before(root)
中序遍历:左节点,根节点,右节点
class MiddleTravel():
    def __init__(self):
        self.list = []

    def Middle(self, root):
        if root:
            self.Middle(root.left)  ##当递归到最底部的时候,在倒回去执行
            self.list.append(root.val)
            self.Middle(root.right)  ##当左节点下面有右节点的时候,上面的递归会一直到上面的根节点(存在右节点的时候),在往下面找左边的右边的节点
        return self.list
res = MiddleTravel().Middle(root)

后序遍历:左节点,右节点,根节点
class AfterTravel():
    def __init__(self):
        self.list = []

    def After(self, root):
        if root:
            self.After(root.left)
            self.After(root.right)
            self.list.append(root.val)
        return self.list
res = AfterTravel().After(root)
# print(res)


层次遍历:
class  Level():
    def  Travel(self,root):
     if not root:
       return []
     if  root:
            res=[]
            queue=[root]##设置每一层的队列
            while queue:
                node_list = []  ###设置每一层节点的值
                for  i in  range(0,len(queue)):
                    node=queue.pop(0)###一直在下面取值,拿出队列里面所有的值出来,每一层队列所有的值,放进新的队列里面
                    node_list.append(node.val)
                    if  node.left:
                        queue.append(node.left)
                    if  node.right:
                        queue.append(node.right)
                res.append(node_list)
            return  res
# res=Level().Travel(root)
# print(res)


找到二叉树所有的路径:
class   all_route():
    def  __init__(self):
        self.list=[]
    def  all(self,root,ele):
        if  root:
            ele+=str(root.val)
            if root.left==None  and root.right==None:
                self.list.append(ele)
                print(self.list)
            if  root.left:
                self.all(root.left,ele+'->')
            if  root.right:
                self.all(root.right,ele+'->')
all_route().all(root,'')

找到二叉树路径和等于指定的值的所有路径

'''最大路径和'''
class  Solution():
    def  __init__(self):
        self.res=[]
        self.valid_list=[]
    def   max_route(self,root,target):
        if  root  is  None:
            return   self.res##最终返回结果
        # print('target',target)
        self.valid_list.append(root.val)##每一次循环加上root的值进来,加上
        target-=root.val##每一次循环减去root相对应的值
        if target==0  and   root.left  is  None  and  root.right  is  None:
            self.res.append(list(self.valid_list))
        self.max_route(root.left,target)##递归完这一层之后开始递归后面的这层
        # print('target_next',target)
        self.max_route(root.right,target)
        self.valid_list.pop()##移除掉最后的一个值出来
        return  self.res
root=Tree(1)
root.left=Tree(5)
root.right=Tree(5)
root.left.left=Tree(1)
root.left.right=Tree(1)
root.right.left=Tree(1)
root.right.left.left=Tree(1)
res=Solution().max_route(root,7)
原文地址:https://www.cnblogs.com/yunxintryyoubest/p/10319404.html