• leetcode1367


     1 class Solution:
     2     def __init__(self):
     3         self.l = []
     4 
     5     def preOrder(self,node,temp):
     6         if node != None:
     7             temp.append(str(node.val))
     8             if node.left != None:
     9                 self.preOrder(node.left,temp)
    10             if node.right != None:
    11                 self.preOrder(node.right,temp)
    12             if node.left == None and node.right == None:
    13                 s = '|'.join(temp)
    14                 self.l.append(s)
    15             temp.pop(-1)
    16 
    17 
    18     def isSubPath(self, head: 'ListNode', root: 'TreeNode') -> bool:
    19         self.preOrder(root,[])
    20         #print(self.l)
    21         comp = []
    22         while head != None:
    23             comp.append(str(head.val))
    24             head = head.next
    25         target = '|'.join(comp)
    26         for l in self.l:
    27             if l.find(target) >= 0:
    28                 return True
    29         return False

    算法思路:二叉树(先序)遍历,链表顺序遍历。

    二叉树遍历,存储每一个路径的节点集合,将这个集合生成用'|'分割的字符串。

    按顺序遍历链表,也生成字符串。

    循环二叉树的路径集合,判断每一个路径字符串,是否包含head生成的子字符串。

  • 相关阅读:
    Summarizing NUMA Scheduling两篇文章,解释得不错
    VCAP5-DCA – What’s new?
    NUMA总结。
    NUMA and vNUMA
    NUMA
    vsphere 5.1 性能最佳实践。
    vsphere性能
    mysql的事务,隔离级别和锁
    mysql5.7 生成列 generated column
    mysql8 公用表表达式CTE的使用
  • 原文地址:https://www.cnblogs.com/asenyang/p/12389535.html
Copyright © 2020-2023  润新知