LeetCode 1367. Linked List in Binary Tree

原题链接在这里:https://leetcode.com/problems/linked-list-in-binary-tree/

题目:

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.

Example 1:

Input: head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
Output: true
Explanation: Nodes in blue form a subpath in the binary Tree.  

Example 2:

Input: head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
Output: true

Example 3:

Input: head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
Output: false
Explanation: There is no path in the binary tree that contains all the elements of the linked list from head.

Constraints:

  • The number of nodes in the tree will be in the range [1, 2500].
  • The number of nodes in the list will be in the range [1, 100].
  • 1 <= Node.val <= 100 for each node in the linked list and binary tree.

题解:

Check if head == null, return true. That means there must be this list in tree.

Otherwise if root == null, return false.

Then if current head val == root val, then we could check root left and right with head next.

No matter head val == or != root val, we still need to check root with head next.

Time Complexity: O(n * min(len, height)). n is number of nodes in tree. height is tree height. len is list length.

Space: O(height). stack space.

AC Java:

复制代码
 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode() {}
 7  *     ListNode(int val) { this.val = val; }
 8  *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 9  * }
10  */
11 /**
12  * Definition for a binary tree node.
13  * public class TreeNode {
14  *     int val;
15  *     TreeNode left;
16  *     TreeNode right;
17  *     TreeNode() {}
18  *     TreeNode(int val) { this.val = val; }
19  *     TreeNode(int val, TreeNode left, TreeNode right) {
20  *         this.val = val;
21  *         this.left = left;
22  *         this.right = right;
23  *     }
24  * }
25  */
26 class Solution {
27     public boolean isSubPath(ListNode head, TreeNode root) {
28         if(head == null){
29             return true;
30         }
31         
32         if(root == null){
33             return false;
34         }
35         
36         boolean res = false;
37         if(head.val == root.val){
38             res = res || isSubPath(head.next, root.left) || isSubPath(head.next, root.right);
39         }
40         
41         res = res || isSubPath(head, root.left) || isSubPath(head, root.right);
42         return res;
43     }
44 }
复制代码

Here we could apply KMP.

Time Complexity: O(n + len).

Space: O(len + height).

AC Java:

复制代码
 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode() {}
 7  *     ListNode(int val) { this.val = val; }
 8  *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 9  * }
10  */
11 /**
12  * Definition for a binary tree node.
13  * public class TreeNode {
14  *     int val;
15  *     TreeNode left;
16  *     TreeNode right;
17  *     TreeNode() {}
18  *     TreeNode(int val) { this.val = val; }
19  *     TreeNode(int val, TreeNode left, TreeNode right) {
20  *         this.val = val;
21  *         this.left = left;
22  *         this.right = right;
23  *     }
24  * }
25  */
26 class Solution {
27     public boolean isSubPath(ListNode head, TreeNode root) {
28         if(head == null){
29             return true;
30         }
31         
32         if(root == null){
33             return false;
34         }
35         
36         List<Integer> listVals = new ArrayList<>();
37         List<Integer> listInds = new ArrayList<>();
38         int ind = 0;
39         listInds.add(0);
40         listVals.add(head.val);
41         head = head.next;
42         while(head != null){
43             while(ind > 0 && head.val != listVals.get(ind)){
44                 ind = listInds.get(ind - 1);
45             }
46             
47             if(head.val == listVals.get(ind)){
48                 ind++;
49             }
50             
51             listInds.add(ind);
52             listVals.add(head.val);
53             head = head.next;
54         }
55         
56         return dfs(root, 0, listVals, listInds);
57     }
58     
59     private boolean dfs(TreeNode root, int ind, List<Integer> listVals, List<Integer> listInds){
60         if(root == null){
61             return false;
62         }
63         
64         while(ind > 0 && root.val != listVals.get(ind)){
65             ind = listInds.get(ind - 1);
66         }
67         
68         if(root.val == listVals.get(ind)){
69             ind++;
70         }
71         
72         return ind == listInds.size() || dfs(root.left, ind, listVals, listInds) || dfs(root.right, ind, listVals, listInds);
73     }
74 }
复制代码

 

posted @   Dylan_Java_NYC  阅读(28)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2017-10-03 LeetCode 376. Wiggle Subsequence
点击右上角即可分享
微信分享提示