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)

原文地址:https://www.cnblogs.com/panweiwei/p/13585969.html