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

    package LeetCode_1367
    
    import LeetCode_1382.TreeNode
    import LeetCode_390.ListNode
    
    /**
     * 1367. Linked List in Binary Tree
     * https://leetcode.com/problems/linked-list-in-binary-tree/description/
     *
     * Time complexity: O(head.size * root.size)
     * Space complexity: O(root.size)
     * */
    class Solution {
        fun isSubPath(head: ListNode?, root: TreeNode?): Boolean {
            if (root == null) {
                return false
            }
            return isPath(head, root) || isSubPath(head, root.left) || isSubPath(head, root.right)
        }
    
        private fun isPath(head: ListNode?, root: TreeNode?): Boolean {
            if (head == null) {//if head is null, check the next one
                return true
            }
            if (root == null) {
                return false
            }
            if (root.`val` != head.`val`) {
                return false
            }
            return isPath(head.next, root.left) || isPath(head.next, root.right)
        }
    }
  • 相关阅读:
    再见OI,AFO
    时间复杂度
    NOIP真题:矩阵取数问题
    [USACO12FEB]附近的牛Nearby Cows
    合唱队
    子串
    ZJOI2010基站选址
    分治FFT学习笔记
    「HAOI2018」染色
    「SDOI2015」序列统计
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/12669983.html
Copyright © 2011-2022 走看看