zoukankan      html  css  js  c++  java
  • 【leetcode】1367. Linked List in Binary Tree

    题目如下:

    Given a binary tree root and a linked list with head as the first node. 

    Return True if all the elements in the linked list starting from the head correspond to some downward path connected in the binary tree otherwise return False.

    In this context downward path means a path that starts at some node and goes downwards.

    Example 1:

    Input: head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
    Output: true
    Explanation: Nodes in blue form a subpath in the binary Tree.  
    

    Example 2:

    Input: head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
    Output: true
    

    Example 3:

    Input: head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
    Output: false
    Explanation: There is no path in the binary tree that contains all the elements of the linked list from head.

    Constraints:

    • 1 <= node.val <= 100 for each node in the linked list and binary tree.
    • The given linked list will contain between 1 and 100 nodes.
    • The given binary tree will contain between 1 and 2500 nodes.

    解题思路:依次判断树中每个节点是否能作为链表的头结点即可。

    代码如下:

    # 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):
        res = False
        def compare(self,tree_node,list_node):
            if self.res == True:
                return
            elif list_node.next == None and list_node.val == tree_node.val:
                self.res = True
                return
            elif list_node.val != tree_node.val:
                return
            elif list_node.next == None:
                return
            if tree_node.left != None:
                self.compare(tree_node.left,list_node.next)
            if tree_node.right != None:
                self.compare(tree_node.right,list_node.next)
    
        def isSubPath(self, head, root):
            """
            :type head: ListNode
            :type root: TreeNode
            :rtype: bool
            """
            self.res = False
    
            def recursive(tree_node):
                self.compare(tree_node,head)
                if self.res == True:
                    return
                if tree_node.left != None:
                    recursive(tree_node.left)
                if tree_node.right != None:
                    recursive(tree_node.right)
    
            recursive(root)
            return self.res
  • 相关阅读:
    C# 单例模式
    [C# 线程处理系列]专题四:线程同步
    C#实现远程开机(局域网测试通过)
    C#实现类只实例化一次(被多个类访问调用)
    c#委托与事件、消息、WndProc用法(转)
    UML 类图常用表示方法.
    C# Socket 接受数据不全的处理
    执行数据库操作失败: 连接未关闭。 连接的当前状态为打开
    hdu 2018 母牛的故事 动态规划入门题
    Codeforces 1029B. Creating the Contest 动态规划O(nlogn)解法 及 单调队列O(n)解法
  • 原文地址:https://www.cnblogs.com/seyjs/p/12416400.html
Copyright © 2011-2022 走看看