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)
        }
    }
  • 相关阅读:
    CF1442E. Black, White and Grey Tree
    CF1442D. Sum
    CF1444D. Rectangular Polyline
    arc107F
    外心与垂心关系
    CF1434E. A Convex Game
    CF1434D. Roads and Ramen
    arc106E
    arc106F
    CF704E. Iron Man
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/12669983.html
Copyright © 2011-2022 走看看