首页 > 其他分享 >LeetCode 1367. Linked List in Binary Tree

LeetCode 1367. Linked List in Binary Tree

时间:2022-10-03 17:03:52浏览次数:88  
标签:Binary head TreeNode val List Tree ind null root

原题链接在这里: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 }

 

标签:Binary,head,TreeNode,val,List,Tree,ind,null,root
From: https://www.cnblogs.com/Dylan-Java-NYC/p/16750730.html

相关文章

  • ubuntu 使用sudo vim /etc/apt/sources.list命令修改文件后该如何退出?
    ubuntu使用sudovim/etc/apt/sources.list命令修改文件后该如何退出? ubuntu使用sudovim/etc/apt/sources.list命令修改文件后该如何退出? Esc输入冒号(即shif......
  • Collections之 Arraylist源码解读(二)
    ......
  • CF375D Tree and Queries dsu on tree
    一颗以1为根树,每个点有颜色,每次询问求x子树内颜色出现次数>=k的数量。线段树合并很难处理,考虑dfuontree。直接的想法是开一个数量树状数组每次查一下1~k-1的前缀和来......
  • CF1624G MinOr Tree 题解
    CF1624GMinOrTree给定\(n\)个点,\(m\)条边,求在或运算小的最小生成树考虑二进制拆位,从高位玩往地位贪心,如果答案第\(i\)位可以为\(0\),后\(i-1\)取值无论是多少......
  • leetcode -- tree 4
    不同的二叉搜索树II递归#Definitionforabinarytreenode.#classTreeNode:#def__init__(self,val=0,left=None,right=None):#self.val=......
  • src/delly.h:8:42: fatal error: boost/graph/adjacency_list.hpp: No such file or d
     001、问题src/delly.h:8:42:fatalerror:boost/graph/adjacency_list.hpp:Nosuchfileordirectory  002、解决方法(py37)[root@localhostdelly-1.1.......
  • ArrayList的常用方法
    创建ArrayList变量ArrayList<Integer>arr=newArrayList<>();将ArrayList的数据存到另一个ArrayList(替换原有数据)arr2=arr1;将ArrayList的数据添加到另一个Ar......
  • [ 数据结构 - C++]红黑树RBTree
    在上篇文章我们了解了第一种平衡二叉搜索树AVL树,我们知道AVL树是通过平衡因子来控制左右子树高度差,从而将二叉树变成一颗平衡二叉搜索树。本篇文章我们将要了解另外一种平衡......
  • Collections-Arraylist源码解读(一)
    ......
  • New Year Tree
    NewYearTreeTranslation给出一棵\(n\)个节点的树,根节点为\(1\)。每个节点上有一种颜色\(c_i\)。\(m\)次操作。操作有两种:1uc:将以\(u\)为根的子树上的所有节点......