剑指offer:22. 23. 24 (12.27)

'''
22.从上往下打印二叉树
**题目:**从上往下打印出二叉树的每个节点,同层节点从左至右打印。

**思路:**递归,每次将左子树结果和右子树结果存到结果集之中。
'''
# -*- coding:utf-8 -*-
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution:
    # 返回从上到下每个节点值列表,例:[1,2,3]
    def PrintFromTopToBottom(self, root):
        # write code here
        if root is None:
            return []
        ans=[]
        ans.append(root.val)
        self.orderans(root,ans)
        return ans
    def orderans(self,root,ans):
        if not root:
            return
        if root.left:
            ans.append(root.left.val)
        if root.right:
            ans.append(root.right.val)

        self.orderans(root.left, ans)
        self.orderans(root.right,ans)

if __name__=='__main__':
    solution=Solution()
    A1 = TreeNode(1)
    A2 = TreeNode(2)
    A3 = TreeNode(3)
    A4 = TreeNode(4)
    A5 = TreeNode(5)

    A1.left=A2
    A1.right=A3
    A2.left=A4
    A2.right=A5
    ans=solution.PrintFromTopToBottom(A1)
    print(ans)
'''
23.二叉树的后续遍历序列

**题目:**输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。

**思路:**二叉搜索树的特性是所有左子树值都小于中节点,所有右子树的值都大于中节点,递归遍历左子树和右子树的值。

'''
# -*- coding:utf-8 -*-
class Solution:
    def VerifySquenceOfBST(self, sequence):
        if not sequence:
            return False
        if len(sequence)==1:
            return True
        i=0
        while sequence[i]<sequence[-1]:
            i=i+1
        k=i    #找到左子树的点
        for j in range(i,len(sequence)-1):
            if sequence[j]<sequence[-1]:
                return False
        leftsequence=sequence[:k]
        rightsequence=sequence[k:len(sequence)-1]   #当K=i时已经不满足了
    
        if len(leftsequence)>0:
            a1=self.VerifySquenceOfBST(leftsequence)
        if len(rightsequence)>0:
            a2=self.VerifySquenceOfBST(rightsequence)
        return a1 and a2

if __name__=='__main__':
    solution=Solution()
    num=list(map(int,input().split(' ')))
    ans=solution.VerifySquenceOfBST(num)
    print(ans)
'''
24.二叉树中和为某一值的路径
**题目:**输入一颗二叉树的根节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。
路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。(注意: 在返回值的list中,数组长度大的数组靠前)。

**思路:**利用递归的方法,计算加左子树和右子树之后的值,当参数较多是,可以将结果添加到函数变量之中。

'''
# -*- coding:utf-8 -*-
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution:
    # 返回二维列表,内部每个列表表示找到的路径
    def FindPath(self, root, expectNumber):
        # write code here
        if not root:
            return []
        ans=[]
        path=[]
        self.dfs(root,expectNumber,ans,path)
        ans.sort()
        return ans

    def dfs(self,root,target,ans,path):
        if not root:
            return

        path.append(root.val)
        if root.left is None and root.right is None and target==root.val:
            ans.append(path[:])

        if root.left:
            self.dfs(root.left,target-root.val,ans,path)
        if root.right:
            self.dfs(root.right,target-root.val,ans,path)

        path.pop()


if __name__=='__main__':
    A1=TreeNode(10)
    A2=TreeNode(8)
    A3=TreeNode(12)
    A4=TreeNode(4)
    A5=TreeNode(2)
    A6=TreeNode(2)

    A1.left=A2
    A1.right=A3
    A2.left=A4
    A2.right=A5
    A5.left=A6

    expectNumber=22
    solution=Solution()
    ans=solution.FindPath(A1,expectNumber)
    print(ans)

猜你喜欢

转载自blog.csdn.net/weixin_42234472/article/details/85309775
今日推荐