/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isSubtree(s *TreeNode, t *TreeNode) bool {
if t == nil {
return true
}
if s == nil {
return false
}
// PreOrder traversal
// At C) Check if the head match for both trees, then check if they are identical
// At L & R) Check with s Left subtree with t, r Right subtree with t
if s.Val == t.Val && areIdentical(s, t) {
return true
}
// Keep checkig Tree on L or R subtree
return (isSubtree(s.Left, t) || isSubtree(s.Right, t))
}
func areIdentical(s *TreeNode, t *TreeNode) bool {
if s == nil && t == nil {
return true
}
if s == nil || t == nil {
return false
}
// PreOrder Traversal
// Constrant for C) values of s and t should be same
// Same constraint for L & R) L & R subtree should be same
if s.Val != t.Val {
return false
}
return areIdentical(s.Left, t.Left) && areIdentical(s.Right, t.Right)
}| Subtree of Another Tree |
1268 62.0% Medium
--
Jan 2020 : 4 FAANG OFFERS. 2019: 15 FAANG OFFERS
https://www.techbayarea.us/
whatsapp group : https://chat.whatsapp.com/GxL7anhP9swEWgAsntkrm8
---
You received this message because you are subscribed to the Google Groups "leetcode-meetup" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leetcode-meet...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/leetcode-meetup/0c1afcbe-857c-474c-b263-aec3b79c5edd%40googlegroups.com.
--
Jan 2020 : 4 FAANG OFFERS. 2019: 15 FAANG OFFERS
https://www.techbayarea.us/
whatsapp group : https://chat.whatsapp.com/GxL7anhP9swEWgAsntkrm8
---
You received this message because you are subscribed to the Google Groups "leetcode-meetup" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leetcode-meet...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/leetcode-meetup/20031305-cb28-445c-8e74-ff297b318409%40googlegroups.com.
--
Jan 2020 : 4 FAANG OFFERS. 2019: 15 FAANG OFFERS
https://www.techbayarea.us/
whatsapp group : https://chat.whatsapp.com/GxL7anhP9swEWgAsntkrm8
---
You received this message because you are subscribed to the Google Groups "leetcode-meetup" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leetcode-meet...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/leetcode-meetup/bd705979-52a2-49e1-822f-01f0878e0fb1%40googlegroups.com.
| 99 | Recover Binary Search Tree | 38.5% | Hard |
Runtime: 276 ms, faster than 30.08% of Python3 online submissions for Subtree of Another Tree.Memory Usage: 14.4 MB, less than 50.00% of Python3 online submissions for Subtree of Another Tree.
# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = rightclass Solution:
def isSame(self, root1, root2):if root1 == None and root2 == None:return Trueif root1 != None and root2 != None:if root1.val == root2.val:return self.isSame(root1.left, root2.left) and self.isSame(root1.right, root2.right)return False
def isSubtree(self, s: TreeNode, t: TreeNode) -> bool:
if s == None:return Falseif s.val == t.val:if self.isSame(s,t):return Truereturn self.isSubtree(s.left,t) or self.isSubtree(s.right,t)
Please work on the problems at home and post your responses to the thread. I'll be hosting a Zoom session next Saturday to answer questions about these problems.
1268 62.0% Medium
Runtime: 108 ms, faster than 66.70% of Python3 online submissions for Search Suggestions System.Memory Usage: 16.4 MB, less than 100.00% of Python3 online submissions for Search Suggestions System.'''This problem can be solved using binary search, sort the products and then find the index where searchWord is getting started each time user types searchWord, once we find the index we have to find the next 3 elements which satisfy searchWord.'''# Time : O(k*LogN) k -> len(searchWord), N -> len(products)# Space :
class Solution:def suggestedProducts(self, products: List[str], searchWord: str) -> List[List[str]]:
products.sort()res = []for i in range(len(searchWord)):local = []lo = 0hi = len(products)-1while(hi - lo > 1):mid = int((lo+hi)/2)if products[mid][:i+1] == searchWord[:i+1]:hi = midelif products[mid][:i+1] > searchWord[:i+1]:hi = mid - 1else:lo = mid + 1if products[lo][:i+1] == searchWord[:i+1]:count = 0k = lowhile(k <= len(products)-1 and products[k][:i+1] == searchWord[:i+1] and count != 3):local.append(products[k])k += 1count += 1res.append(local)else:count = 0k = hiwhile(products[k][:i+1] == searchWord[:i+1] and count != 3):local.append(products[k])k += 1count += 1res.append(local)return res
| 572 | Subtree of Another Tree | 44.0% | Easy |
| 1268 | Search Suggestions System | 62.0% | Medium |
| 99 | Recover Binary Search Tree | 38.5% | Hard |