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