-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path1367-linked-list-in-binary-tree.js
45 lines (43 loc) · 1.39 KB
/
1367-linked-list-in-binary-tree.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* 1367. Linked List in Binary Tree
* https://leetcode.com/problems/linked-list-in-binary-tree/
* Difficulty: Medium
*
* 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.
*/
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {ListNode} head
* @param {TreeNode} root
* @return {boolean}
*/
var isSubPath = function(head, root) {
if (!head) return true;
if (!root) return false;
return checkPath(head, root) || isSubPath(head, root.left) || isSubPath(head, root.right);
function checkPath(listNode, treeNode) {
if (!listNode) return true;
if (!treeNode) return false;
if (listNode.val !== treeNode.val) return false;
return checkPath(listNode.next, treeNode.left) || checkPath(listNode.next, treeNode.right);
}
};