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)
    }
}

 

posted @ 2020-04-09 22:09  johnny_zhao  阅读(178)  评论(0编辑  收藏  举报