其他分享
首页 > 其他分享> > 1367. 二叉树中的列表

1367. 二叉树中的列表

作者:互联网







# Definition for singly-linked list.
class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None

# Definition for a binary tree node.
class TreeNode(object):
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution(object):
    def isSubPath(self, head, root):
        """
        :type head: ListNode
        :type root: TreeNode
        :rtype: bool
        """
        if not head:
            return True
        if not root:
            return False
        if self.helper(head, root):
            return True
        return self.isSubPath(head, root.left) or self.isSubPath(head, root.right)

    def helper(self, head, root):
        if not head:
            return True
        if not root:
            return False
        if head.val != root.val:
            return False
        return self.helper(head.next, root.left) or self.helper(head.next, root.right)


标签:__,head,return,helper,self,列表,1367,二叉树,root
来源: https://www.cnblogs.com/panweiwei/p/13585969.html