Skip to content

Commit 2ef589c

Browse files
committed
Add solution #1367
1 parent 9587a9c commit 2ef589c

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,257 LeetCode solutions in JavaScript
1+
# 1,258 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1040,6 +1040,7 @@
10401040
1363|[Largest Multiple of Three](./solutions/1363-largest-multiple-of-three.js)|Hard|
10411041
1365|[How Many Numbers Are Smaller Than the Current Number](./solutions/1365-how-many-numbers-are-smaller-than-the-current-number.js)|Easy|
10421042
1366|[Rank Teams by Votes](./solutions/1366-rank-teams-by-votes.js)|Medium|
1043+
1367|[Linked List in Binary Tree](./solutions/1367-linked-list-in-binary-tree.js)|Medium|
10431044
1368|[Minimum Cost to Make at Least One Valid Path in a Grid](./solutions/1368-minimum-cost-to-make-at-least-one-valid-path-in-a-grid.js)|Hard|
10441045
1372|[Longest ZigZag Path in a Binary Tree](./solutions/1372-longest-zigzag-path-in-a-binary-tree.js)|Medium|
10451046
1374|[Generate a String With Characters That Have Odd Counts](./solutions/1374-generate-a-string-with-characters-that-have-odd-counts.js)|Easy|
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* 1367. Linked List in Binary Tree
3+
* https://leetcode.com/problems/linked-list-in-binary-tree/
4+
* Difficulty: Medium
5+
*
6+
* Given a binary tree root and a linked list with head as the first node.
7+
*
8+
* Return True if all the elements in the linked list starting from the head correspond to some
9+
* downward path connected in the binary tree otherwise return False.
10+
*
11+
* In this context downward path means a path that starts at some node and goes downwards.
12+
*/
13+
14+
/**
15+
* Definition for singly-linked list.
16+
* function ListNode(val, next) {
17+
* this.val = (val===undefined ? 0 : val)
18+
* this.next = (next===undefined ? null : next)
19+
* }
20+
*/
21+
/**
22+
* Definition for a binary tree node.
23+
* function TreeNode(val, left, right) {
24+
* this.val = (val===undefined ? 0 : val)
25+
* this.left = (left===undefined ? null : left)
26+
* this.right = (right===undefined ? null : right)
27+
* }
28+
*/
29+
/**
30+
* @param {ListNode} head
31+
* @param {TreeNode} root
32+
* @return {boolean}
33+
*/
34+
var isSubPath = function(head, root) {
35+
if (!head) return true;
36+
if (!root) return false;
37+
return checkPath(head, root) || isSubPath(head, root.left) || isSubPath(head, root.right);
38+
39+
function checkPath(listNode, treeNode) {
40+
if (!listNode) return true;
41+
if (!treeNode) return false;
42+
if (listNode.val !== treeNode.val) return false;
43+
return checkPath(listNode.next, treeNode.left) || checkPath(listNode.next, treeNode.right);
44+
}
45+
};

0 commit comments

Comments
 (0)