question_slug
stringlengths
3
77
title
stringlengths
1
183
slug
stringlengths
12
45
summary
stringlengths
1
160
author
stringlengths
2
30
certification
stringclasses
2 values
created_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
updated_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
hit_count
int64
0
10.6M
has_video
bool
2 classes
content
stringlengths
4
576k
upvotes
int64
0
11.5k
downvotes
int64
0
358
tags
stringlengths
2
193
comments
int64
0
2.56k
robot-collisions
✅ Easy C++ Solution
easy-c-solution-by-moheat-fuwq
Code\n\nclass Solution {\n struct Robot {\n int position;\n int health;\n char direction;\n int index;\n };\npublic:\n vect
moheat
NORMAL
2024-07-15T15:57:48.475096+00:00
2024-07-15T15:57:48.475128+00:00
6
false
# Code\n```\nclass Solution {\n struct Robot {\n int position;\n int health;\n char direction;\n int index;\n };\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string& directions) {\n int n = positions.size();\n vector<Robot>...
1
0
['C++']
0
robot-collisions
Robot Collisions
robot-collisions-by-dajonas-68yl
Complexity\n- Time complexity:\n Add your time complexity here, e.g. O(n) \n\n- Space complexity:\n Add your space complexity here, e.g. O(n) \n\n# Code\n\nclas
dajonas
NORMAL
2024-07-15T15:03:16.763926+00:00
2024-07-15T15:03:16.763949+00:00
5
false
# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def survivedRobotsHealths(self, positions: List[int], healths: List[int], directions: str) -> List[int]:\n ar...
1
0
['Python3']
0
robot-collisions
✅💯🔥Very Detailed Approach🎯Beginners Friendly Explanation🎓🧠Extremely Simple And Effective🔥
very-detailed-approachbeginners-friendly-ya89
Intuition & Approach\n## Summary:\n- Sort robots by position to handle collisions in order.\n- Use a stack to track robots and simulate collisions.\n- Update he
Shreyansh--Gupta
NORMAL
2024-07-13T21:44:02.957687+00:00
2024-07-13T21:44:02.957717+00:00
9
false
# Intuition & Approach\n## Summary:\n- Sort robots by position to handle collisions in order.\n- Use a stack to track robots and simulate collisions.\n- Update healths based on collision rules.\n- Store surviving robots\' healths in a map to maintain original order.\n- Construct the final result using the map and retur...
1
0
['Stack', 'C++']
0
robot-collisions
C# Solution for Robot Collisions Problem
c-solution-for-robot-collisions-problem-env98
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem requires simulating the movement and collisions of robots on a line. Each r
Aman_Raj_Sinha
NORMAL
2024-07-13T21:42:17.607692+00:00
2024-07-13T21:42:17.607720+00:00
18
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem requires simulating the movement and collisions of robots on a line. Each robot has a direction (either left or right), a position, and health. The main challenge is to handle the collisions correctly and determine the final h...
1
0
['C#']
0
robot-collisions
Java Solution for Robot Collisions Problem
java-solution-for-robot-collisions-probl-a9y8
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem involves simulating the movement and collisions of robots on a line based o
Aman_Raj_Sinha
NORMAL
2024-07-13T21:33:09.903401+00:00
2024-07-13T21:33:09.903430+00:00
13
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem involves simulating the movement and collisions of robots on a line based on their initial positions, health, and movement directions. The key challenge is to handle collisions between robots moving towards each other, update ...
1
0
['Java']
0
robot-collisions
Group related data into a struct for simpler processing
group-related-data-into-a-struct-for-sim-uou9
txt\nstruct robot\n{\n\tint position;\n\tint health;\n\tchar direction;\n\n\tint originalIndex;\n};\n\nstruct robot input[100001];\n\nvoid\nrobotSurvived(struct
spektrokalter
NORMAL
2024-07-13T21:03:38.029593+00:00
2024-07-13T21:21:11.921618+00:00
14
false
```txt\nstruct robot\n{\n\tint position;\n\tint health;\n\tchar direction;\n\n\tint originalIndex;\n};\n\nstruct robot input[100001];\n\nvoid\nrobotSurvived(struct robot *robots, int *nrobots)\n{\n\tstruct robot *stack = robots;\n\tint nstack = 0;\n\n\tstruct robot *rp;\n\tfor (rp = robots; rp != robots + *nrobots; ++r...
1
0
['C']
0
robot-collisions
🎯✅Detailed and Step-By-Step Java Implementation and usage of Deque as stack
detailed-and-step-by-step-java-implement-9wor
Intuition\n Describe your first thoughts on how to solve this problem. \nBasically, there are robots moving on a same line either left or right. Each of them ha
saraswatanishka
NORMAL
2024-07-13T21:02:23.807355+00:00
2024-07-13T21:02:23.807392+00:00
5
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nBasically, there are robots moving on a same line either *left* or *right*. Each of them have a *health* value determining their health. When they collide,\n- If healths are equal, both are removed from the line.\n- Else, one with the hig...
1
0
['Stack', 'Java']
0
robot-collisions
Sort Positions, and Simulate in a Stack
sort-positions-and-simulate-in-a-stack-b-wth5
\nSort the positions, and then simulate the process using a stack.\n\nclass Solution {\npublic:\n \n vector<int> survivedRobotsHealths(vector<int>& pos, v
tbne1905
NORMAL
2024-07-13T20:13:03.303186+00:00
2024-07-13T20:13:03.303217+00:00
16
false
\nSort the positions, and then simulate the process using a stack.\n```\nclass Solution {\npublic:\n \n vector<int> survivedRobotsHealths(vector<int>& pos, vector<int>& healths, string dirs) {\n int n = pos.size();\n vector<vector<int>> v;\n for (int i=0; i<n;i++){\n int p = pos[i]...
1
0
['C++']
0
robot-collisions
Probably the cleanest code
probably-the-cleanest-code-by-almostmond-vtuu
Intuition\n Describe your first thoughts on how to solve this problem. \nI think the solution is simple, it\'s all about the code. Just sort robots by their pos
almostmonday
NORMAL
2024-07-13T19:54:09.657521+00:00
2024-07-13T20:02:36.987224+00:00
19
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nI think the solution is simple, it\'s all about the code. Just sort robots by their positions and simulate the process.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nLet\'s try to simplify the code.\n\n# Complexi...
1
0
['Array', 'Stack', 'Greedy', 'Sorting', 'Simulation', 'Python', 'Python3']
1
robot-collisions
For my Cutiepiee
for-my-cutiepiee-by-myth64-nfya
\n# Code\n\nclass Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& health, string directions) {\n vector<p
Myth64
NORMAL
2024-07-13T19:16:03.255172+00:00
2024-07-13T19:17:28.898212+00:00
21
false
\n# Code\n```\nclass Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& health, string directions) {\n vector<pair<int,int>> indexMap;\n for(int i1=0;i1<positions.size();++i1){\n indexMap.push_back({positions[i1],i1});\n }\n sort(indexM...
1
0
['C++']
0
robot-collisions
Simple c++ solution
simple-c-solution-by-abhijeet_19403-qpk4
here is the simple c++ code\n\n\n
abhijeet_19403
NORMAL
2024-07-13T19:11:56.825729+00:00
2024-07-13T19:11:56.825760+00:00
9
false
here is the simple c++ code\n```\n\n```
1
0
['C', 'Simulation']
0
robot-collisions
✅EASY JAVA SOLUTION
easy-java-solution-by-swayam28-n9ua
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
swayam28
NORMAL
2024-07-13T19:05:22.002088+00:00
2024-07-13T19:05:22.002120+00:00
19
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
1
0
['Java']
0
robot-collisions
Immortal Robots
immortal-robots-by-manuparmar37-35ro
Intuition\nStack could be implemented to solve this question\n\n# Approach\nTraverse from left to right according to the robots position on the axis.\nMaintain
manuparmar37
NORMAL
2024-07-13T17:59:36.654863+00:00
2024-07-13T17:59:36.654899+00:00
12
false
# Intuition\nStack could be implemented to solve this question\n\n# Approach\nTraverse from left to right according to the robots position on the axis.\nMaintain a stack, which will contain all the robots which point towards are flowing towards right.\nIf the direction is left and the stack is empty then the robot is i...
1
0
['C++']
0
robot-collisions
java solution using Stack & sorting✅✅✅
java-solution-using-stack-sorting-by-i_a-ash5
\n//use as container for better calculation\nclass Node{\n int position,health,originalIndex;\n char direction;\n public Node(int p,int h,int ind,char
I_am_SOURAV
NORMAL
2024-07-13T17:53:11.006072+00:00
2024-07-13T17:53:11.006127+00:00
7
false
```\n//use as container for better calculation\nclass Node{\n int position,health,originalIndex;\n char direction;\n public Node(int p,int h,int ind,char dir){\n position=p;\n health =h;\n originalIndex=ind;\n direction=dir;\n }\n}\n\nclass Solution {\n public List<Integer> su...
1
0
['Array', 'Stack', 'Sorting', 'Java']
0
robot-collisions
Beats 100% 🔥| Clean Approach ✅ with OOP's and Stack Implementation using List | Beast Method ⚔️
beats-100-clean-approach-with-oops-and-s-e8nq
\n\n### Intuition \uD83E\uDDE0\n In this problem, we simulate a series of robots moving in a straight line. Each robot has a position, health, and direction (ei
_one_day_
NORMAL
2024-07-13T17:43:26.185257+00:00
2024-07-19T17:30:43.841871+00:00
31
false
![Screenshot 2024-07-13 225334.png](https://assets.leetcode.com/users/images/480fe362-9713-4df2-ba66-b752cbc4c7d5_1720892216.3761961.png)\n\n### Intuition \uD83E\uDDE0\n In this problem, we simulate a series of robots moving in a straight line. Each robot has a position, health, and direction (either \'R\' for right or...
1
0
['Array', 'Stack', 'C', 'Sorting', 'Simulation', 'Python', 'C++', 'Java']
0
robot-collisions
Code will explain you well (BASIC Stack operations)🐱‍🏍
code-will-explain-you-well-basic-stack-o-u4ru
\u2B06PLEASE UPVOTE IF IT HELPS\u2B06\n\n# Code\n\nclass Solution:\n def survivedRobotsHealths(self, positions: List[int], healths: List[int], directions: st
hacker363
NORMAL
2024-07-13T17:42:38.857549+00:00
2024-07-13T17:42:38.857576+00:00
20
false
**\u2B06PLEASE UPVOTE IF IT HELPS\u2B06**\n\n# Code\n```\nclass Solution:\n def survivedRobotsHealths(self, positions: List[int], healths: List[int], directions: str) -> List[int]:\n l = [[positions[i],healths[i],directions[i],i] for i in range(len(positions))]\n l.sort()\n stack = []\n f...
1
0
['Python3']
0
robot-collisions
beats 100% || C++ || Easy approaches(two approaches, second one is optimal)
beats-100-c-easy-approachestwo-approache-5ivg
Intuition\n Describe your first thoughts on how to solve this problem. \nSimulate the process.\n\n# Approach\n Describe your approach to solving the problem. \n
brightworld
NORMAL
2024-07-13T17:41:30.883272+00:00
2024-07-13T17:41:30.883303+00:00
26
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSimulate the process.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1.Indices Array: We create an array of indices and sort it based on the positions of the robots.\n\n2.Stack Simulation: We use a stack to manage...
1
0
['C++']
1
robot-collisions
Solution using stack and sorting
solution-using-stack-and-sorting-by-maha-sp1y
Code\n\nfunction survivedRobotsHealths(positions: number[], healths: number[], directions: string): number[] {\n enum Direction {\n Right,\n Le
mahandor
NORMAL
2024-07-13T17:41:05.459045+00:00
2024-07-13T17:41:05.459070+00:00
35
false
# Code\n```\nfunction survivedRobotsHealths(positions: number[], healths: number[], directions: string): number[] {\n enum Direction {\n Right,\n Left\n }\n interface Robot {\n position: number,\n health: number,\n direction: Direction,\n inputPosition: number,\n }\...
1
0
['TypeScript']
0
robot-collisions
JAVA Solu with comments !!
java-solu-with-comments-by-harshsharma08-6ata
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
Harshsharma08
NORMAL
2024-07-13T17:36:40.340731+00:00
2024-07-13T17:36:40.340761+00:00
23
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
1
0
['Array', 'Stack', 'Sorting', 'Simulation', 'C++', 'Java']
0
robot-collisions
Beats 99% | Understand with short explanation
beats-99-understand-with-short-explanati-an87
Intuition\nThe problem is very similar to asteroid collisions.\n\n# Approach\n- Process robots by their positions. Hence sort the positions but also maintain th
tsman
NORMAL
2024-07-13T15:47:22.725355+00:00
2024-07-13T15:47:22.725375+00:00
14
false
# Intuition\nThe problem is very similar to [asteroid collisions](https://leetcode.com/problems/asteroid-collision/).\n\n# Approach\n- Process robots by their positions. Hence sort the positions but also maintain their original index of the given array in `second`:\n\n ```cpp\n vector<pair<int, int>> positionsVal...
1
0
['C++']
0
robot-collisions
Check it || Simple C++ Solution
check-it-simple-c-solution-by-coding_kar-amx9
\n# Code\n\nclass Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n vector<
coding_kar_le_bhai
NORMAL
2024-07-13T15:43:41.410227+00:00
2024-07-13T15:43:41.410245+00:00
51
false
\n# Code\n```\nclass Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n vector<int> ans;\n vector<pair<int, int>> robo;\n int n = positions.size();\n for(int i=0; i<n; i++)\n {\n robo.push_back({po...
1
0
['Stack', 'Sorting', 'C++']
0
robot-collisions
Robot wars
robot-wars-by-umarf22-eine
Intuition\n Describe your first thoughts on how to solve this problem. \nStandard stack based solution.\n\n# Approach\n Describe your approach to solving the pr
umarf22
NORMAL
2024-07-13T15:36:01.559564+00:00
2024-07-13T15:36:01.559592+00:00
10
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nStandard stack based solution.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nUse stack to process the collisions.\n\nConsider all these cases:\n1. Robotos who never collided (all the first L\'s)\n2. Robots who mi...
1
0
['Python3']
1
robot-collisions
Solution using Class and stack
solution-using-class-and-stack-by-924ans-0u7t
Intuition\n Describe your first thoughts on how to solve this problem. \nSince there are many things to link together i thought of classes.\nThis is not the bes
924anshuchaurasia
NORMAL
2024-07-13T15:33:13.123144+00:00
2024-07-13T15:33:13.123166+00:00
22
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSince there are many things to link together i thought of classes.\nThis is not the best i know but here we go...\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nDefine the Robot Class.\nWe create a list of Robot o...
1
0
['Stack', 'C++']
0
subtree-of-another-tree
Java Solution, tree traversal
java-solution-tree-traversal-by-shawngao-9x3i
For each node during pre-order traversal of s, use a recursive function isSame to validate if sub-tree started with this node is the same with t.\n\npublic clas
shawngao
NORMAL
2017-05-07T03:08:02.020000+00:00
2018-10-15T05:14:35.396669+00:00
134,606
false
For each node during pre-order traversal of ```s```, use a recursive function ```isSame``` to validate if sub-tree started with this node is the same with ```t```.\n```\npublic class Solution {\n public boolean isSubtree(TreeNode s, TreeNode t) {\n if (s == null) return false;\n if (isSame(s, t)) retur...
739
9
[]
91
subtree-of-another-tree
Python, Straightforward with Explanation (O(ST) and O(S+T) approaches)
python-straightforward-with-explanation-3p3k9
Naive approach, O(|s| * |t|)\nFor each node of s, let's check if it's subtree equals t. We can do that in a straightforward way by an isMatch function: check i
awice
NORMAL
2017-05-07T03:13:47.989000+00:00
2018-10-26T04:41:26.926788+00:00
82,992
false
**Naive approach, O(|s| * |t|)**\nFor each node of ```s```, let's check if it's subtree equals ```t```. We can do that in a straightforward way by an ```isMatch``` function: check if ```s``` and ```t``` match at the values of their roots, plus their subtrees match. Then, in our main function, we want to check if ```s...
548
9
[]
70
subtree-of-another-tree
[Java/Python] 2 solutions: Naive, Serialize in Preorder then KMP - O(M+N) - Clean & Concise
javapython-2-solutions-naive-serialize-i-zm3s
\u2714\uFE0F Solution 1: Naive Solution\n\n\nComplexity\n- Time: O(M * N), where M is the number of nodes in binary tree root, N is the number of nodes in binar
hiepit
NORMAL
2020-01-08T17:01:16.473036+00:00
2021-09-28T13:11:01.987825+00:00
16,211
false
**\u2714\uFE0F Solution 1: Naive Solution**\n<iframe src="https://leetcode.com/playground/Ub9cYzDZ/shared" frameBorder="0" width="100%" height="300"></iframe>\n\n**Complexity**\n- Time: `O(M * N)`, where `M` is the number of nodes in binary tree `root`, `N` is the number of nodes in binary tree `subRoot`\n- Space: `O(H...
180
1
[]
17
subtree-of-another-tree
Short Python by converting into strings
short-python-by-converting-into-strings-ojgxd
Basically we convert our tree into string representation, then just check whether substring exists in target string.\n\nclass Solution(object):\n def isSubtr
realisking
NORMAL
2017-05-07T03:28:12.175000+00:00
2018-10-12T03:59:37.723151+00:00
24,969
false
Basically we convert our ```tree``` into ```string``` representation, then just check whether substring exists in target string.\n```\nclass Solution(object):\n def isSubtree(self, s, t):\n """\n :type s: TreeNode\n :type t: TreeNode\n :rtype: bool\n """\n def convert(p):\n ...
165
7
[]
20
subtree-of-another-tree
19ms C++ solution beats 99.9%
19ms-c-solution-beats-999-by-vicch7-ie1p
Two trees are identical when they are recursively identical. Brute force solution would be recursively compare each node in s with t to check for identical. Bet
vicch7
NORMAL
2017-06-02T13:26:36.474000+00:00
2017-06-02T13:26:36.474000+00:00
36,061
false
Two trees are identical when they are recursively identical. Brute force solution would be recursively compare each node in s with t to check for identical. Better solution is to only compare nodes in `s` with the same max depth as `t`. First get max depth of `t`, then recursively check each node in `s`, if depth equal...
138
4
[]
28
subtree-of-another-tree
Python Easy to Understand
python-easy-to-understand-by-ccparameciu-wb7w
```\nclass Solution:\n def isSubtree(self, s: TreeNode, t: TreeNode) -> bool:\n if not s: \n return False\n if self.isSameTree(s, t)
ccparamecium
NORMAL
2019-03-30T18:13:08.162891+00:00
2019-03-30T18:13:08.162920+00:00
26,061
false
```\nclass Solution:\n def isSubtree(self, s: TreeNode, t: TreeNode) -> bool:\n if not s: \n return False\n if self.isSameTree(s, t): \n return True\n return self.isSubtree(s.left, t) or self.isSubtree(s.right, t)\n\n def isSameTree(self, p: TreeNode, q: TreeNode) -> boo...
109
3
['Python', 'Python3']
17
subtree-of-another-tree
Python dfs both tree at same time
python-dfs-both-tree-at-same-time-by-xdo-06nj
First, travel down the bigger tree via standard dfs, if we find node equal to the value of root of the smaller tree, compare the subtrees.\n\nWe travel down bot
davidzenghi
NORMAL
2018-08-03T15:35:58.954576+00:00
2018-09-02T19:18:04.223658+00:00
18,705
false
First, travel down the bigger tree via standard dfs, if we find node equal to the value of root of the smaller tree, compare the subtrees.\n\nWe travel down both subtrees at the same time and if and only if every node is the same then we know we have found the right subtree.\n\n```\nclass Solution(object):\n def isS...
108
2
[]
17
subtree-of-another-tree
C++ solution using tree traversal with explanation (explained)
c-solution-using-tree-traversal-with-exp-0jxp
\n In this question we need to check if t is a subtree of s. So we will start with comparing between s and t itself.\nIf they are equal then yay we got our subt
shweta_k
NORMAL
2021-04-16T06:33:18.790052+00:00
2021-04-16T06:33:18.790113+00:00
14,649
false
\n In this question we need to check if t is a subtree of s. So we will start with comparing between s and t itself.\nIf they are equal then yay we got our subtree. \nelse we need to pass the left and right nodes of s and check if they are subtree.\nWe will perform the same thing till we reach end of s or subtree is fo...
79
8
['C', 'C++']
17
subtree-of-another-tree
Easy O(n) java solution using preorder traversal
easy-on-java-solution-using-preorder-tra-zxvw
```\npublic class Solution {\n public boolean isSubtree(TreeNode s, TreeNode t) {\n String spreorder = generatepreorderString(s); \n String tpreor
giridhar_bhageshpur
NORMAL
2017-05-08T04:18:12.800000+00:00
2018-10-18T00:18:21.914741+00:00
32,009
false
```\npublic class Solution {\n public boolean isSubtree(TreeNode s, TreeNode t) {\n String spreorder = generatepreorderString(s); \n String tpreorder = generatepreorderString(t);\n \n return spreorder.contains(tpreorder) ;\n }\n public String generatepreorderString(TreeNode s){\n ...
72
9
[]
17
subtree-of-another-tree
c++ recursive
c-recursive-by-duoduo-i38k
referred from https://discuss.leetcode.com/topic/88508/java-solution-tree-traversal\n\n\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *
duoduo
NORMAL
2017-06-29T17:19:05.263000+00:00
2018-09-03T08:42:42.828607+00:00
12,736
false
referred from https://discuss.leetcode.com/topic/88508/java-solution-tree-traversal\n\n```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npubli...
69
3
[]
9
subtree-of-another-tree
Python 98% speed with comments
python-98-speed-with-comments-by-whitese-fta0
So this is just the implementation of the 1. solution in the problem.\nWe make a recursive traverse_tree function that will return a string representation of a
whiteseraph
NORMAL
2019-09-20T09:26:32.397945+00:00
2019-09-20T09:26:32.397985+00:00
17,498
false
So this is just the implementation of the 1. solution in the problem.\nWe make a recursive `traverse_tree` function that will return a string representation of a tree, and then we just need to convert both trees to strings and check if tree `t` is substring of tree `s`\n```\nclass Solution:\n def isSubtree(self, s: ...
65
1
['Python', 'Python3']
17
subtree-of-another-tree
[Java] Recursion (Node Comparison) & Preorder Sequence Comparison (StringBuilder)
java-recursion-node-comparison-preorder-16vt0
Reference: LeetCode\nDifficulty: Easy\n\n## Problem\n\n> Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and node
junhaowanggg
NORMAL
2019-11-19T19:33:17.925149+00:00
2019-11-19T19:33:17.925197+00:00
8,361
false
Reference: [LeetCode](https://leetcode.com/problems/subtree-of-another-tree/)\nDifficulty: <span class="green">Easy</span>\n\n## Problem\n\n> Given two non-empty binary trees `s` and `t`, check whether tree `t` has exactly the same structure and node values with a subtree of `s`. A subtree of `s` is a tree consists of ...
64
0
['Depth-First Search', 'Recursion', 'Java']
3
subtree-of-another-tree
Concise JavaScript solution using DFS
concise-javascript-solution-using-dfs-by-0s9c
\nvar isSubtree = function(s, t) {\n if (!s) return !t;\n return isEqual(s, t) || isSubtree(s.left, t) || isSubtree(s.right, t);\n};\n\nfunction isEqual(r
loctn
NORMAL
2017-05-07T03:47:05.832000+00:00
2018-10-25T08:29:06.015486+00:00
6,958
false
```\nvar isSubtree = function(s, t) {\n if (!s) return !t;\n return isEqual(s, t) || isSubtree(s.left, t) || isSubtree(s.right, t);\n};\n\nfunction isEqual(root1, root2) {\n if (!root1 || !root2) return !root1 && !root2;\n if (root1.val !== root2.val) return false;\n return isEqual(root1.left, root2.left...
48
4
['JavaScript']
5
subtree-of-another-tree
Java Concise O(n+m) Time O(n+m) Space
java-concise-onm-time-onm-space-by-compt-d5ad
\npublic boolean isSubtree(TreeNode s, TreeNode t) {\n return serialize(s).contains(serialize(t)); // Java uses a naive contains algorithm so to ensure linea
compton_scatter
NORMAL
2017-05-07T03:02:49.122000+00:00
2017-05-07T03:02:49.122000+00:00
15,612
false
```\npublic boolean isSubtree(TreeNode s, TreeNode t) {\n return serialize(s).contains(serialize(t)); // Java uses a naive contains algorithm so to ensure linear time, \n // replace with KMP algorithm\n}\n\npublic String serialize(TreeNode root) {\n StringBuilder res...
42
6
[]
11
subtree-of-another-tree
Java | Time O(n) | Space O(h) | DFS | Explain
java-time-on-space-oh-dfs-explain-by-wil-579y
\nThe conditions are:\n1. The tree itself can be a subtree.\n2. A subtree is a tree that consists of a node in the root tree and all of its descendants.\n3. Nod
williamhsucs
NORMAL
2022-01-28T15:24:03.378883+00:00
2022-01-29T10:33:49.914721+00:00
6,203
false
\nThe conditions are:\n1. The tree itself can be a subtree.\n2. A subtree is a tree that consists of a node in the root tree and all of its descendants.\n3. Node values can be duplicated.\n\nList some examples:\n\n![image](https://assets.leetcode.com/users/images/b4bc2e14-50f9-4ffd-938a-99d188b54fb7_1643379595.473873.p...
41
1
['Depth-First Search', 'Java']
6
subtree-of-another-tree
✅💯🔥Simple Code🚀📌|| 🔥✔️Easy to understand🎯 || 🎓🧠Beats 100%🔥|| Beginner friendly💀💯
simple-code-easy-to-understand-beats-100-hqc6
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
atishayj4in
NORMAL
2024-07-26T20:33:23.487451+00:00
2024-08-01T19:56:36.851297+00:00
7,071
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
40
0
['Tree', 'Depth-First Search', 'C', 'String Matching', 'Binary Tree', 'Hash Function', 'Python', 'C++', 'Java']
2
subtree-of-another-tree
Depth-First Search✅ | O( n^2)✅ | Python(Step by step explanation)✅
depth-first-search-o-n2-pythonstep-by-st-i5iw
Intuition\nThe problem is to determine if one binary tree (subRoot) is a subtree of another binary tree (root). A subtree of a tree T is a tree consisting of a
monster0Freason
NORMAL
2023-11-10T18:26:58.351513+00:00
2023-11-10T18:26:58.351551+00:00
4,753
false
# Intuition\nThe problem is to determine if one binary tree (subRoot) is a subtree of another binary tree (root). A subtree of a tree T is a tree consisting of a node in T and all of its descendants. The intuition is to perform a recursive comparison for each node in the root tree. If any subtree of the root tree match...
39
0
['Tree', 'Depth-First Search', 'Recursion', 'Python3']
1
subtree-of-another-tree
✅Accepted | | ✅Easy solution || ✅Short & Simple || ✅Best Method
accepted-easy-solution-short-simple-best-lfqu
\n# Code\n\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode(
sanjaydwk8
NORMAL
2023-01-20T15:13:57.625706+00:00
2023-01-20T15:13:57.625746+00:00
4,790
false
\n# Code\n```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeN...
32
0
['C++']
2
subtree-of-another-tree
JavaScript | 98% Solutions for BFS and DFS
javascript-98-solutions-for-bfs-and-dfs-roxsb
Doing the Blind 75 and posting all solutions.\n\nThis one\'s not too bad if you break it down into subproblems. First, you need a way to check if two trees are
casmith1987
NORMAL
2021-09-22T21:12:00.259534+00:00
2021-09-23T18:31:10.237980+00:00
5,088
false
Doing the Blind 75 and posting all solutions.\n\nThis one\'s not too bad if you break it down into subproblems. First, you need a way to check if two trees are equal. Write a helper function that accomplishes this task (If you\'re struggling here LC 100 is this exact problem. Find a solution, and paste here as a hel...
31
0
['JavaScript']
5
subtree-of-another-tree
No Recursion, No Traverse
no-recursion-no-traverse-by-automedon-eva8
\nvar isSubtree = function(s, t) {\n return JSON.stringify(s).indexOf(JSON.stringify(t))!==-1\n};\n
automedon
NORMAL
2020-05-03T15:36:59.043788+00:00
2020-05-03T15:36:59.043845+00:00
2,102
false
```\nvar isSubtree = function(s, t) {\n return JSON.stringify(s).indexOf(JSON.stringify(t))!==-1\n};\n```
31
3
['JavaScript']
4
subtree-of-another-tree
Short C++ solution
short-c-solution-by-whoan-29br
I couldn\'t get simpler than this. I hope code is self-explanatory:\n\ncpp\nclass Solution {\npublic:\n bool areEqual(TreeNode* s, TreeNode* t) {\n if (s ==
whoan
NORMAL
2019-11-10T20:15:56.093189+00:00
2019-11-10T20:17:02.311956+00:00
4,089
false
I couldn\'t get simpler than this. I hope code is self-explanatory:\n\n```cpp\nclass Solution {\npublic:\n bool areEqual(TreeNode* s, TreeNode* t) {\n if (s == nullptr || t == nullptr) {\n return s == t; // both null\n }\n return s->val == t->val\n && areEqual(s->left, t->left)\n && areEqual(s...
31
0
['C']
8
subtree-of-another-tree
[Animated Video] Easy O(N+M) Serialized Optimal Solution VISUALIZED
animated-video-easy-onm-serialized-optim-7swb
CodeInMotion ResourcesEvery Leetcode Pattern You Need To Knowhttps://www.blog.codeinmotion.io/p/leetcode-patternsBlind 75 Animated Playlisthttps://www.youtube.c
codeinmotion
NORMAL
2025-01-22T01:01:26.588439+00:00
2025-01-22T01:01:26.588439+00:00
3,947
false
![Code In Motion Purple Transparent Background (2).png](https://assets.leetcode.com/users/images/69a60a6b-9c0f-4aa1-899b-98ee5f9b1dc1_1733778334.1189735.png) # *CodeInMotion Resources* ## Every Leetcode Pattern You Need To Know https://www.blog.codeinmotion.io/p/leetcode-patterns ## Blind 75 Animated Playlist https:/...
28
0
['Tree', 'String Matching', 'Binary Tree', 'Hash Function', 'C++', 'Java', 'Go', 'Python3', 'JavaScript', 'C#']
3
subtree-of-another-tree
clean code &5 python methods: bfs(preorder, postorder, inorder, level bfs), dfs
clean-code-5-python-methods-bfspreorder-gg7z4
DFS is the slowest (240ms) method, BFS methods seems are all same ( all about 150ms and faster than 80+%).\nI think from short length can accelerate which means
dustlihy
NORMAL
2021-04-25T21:27:34.613341+00:00
2021-04-25T22:29:57.179910+00:00
2,441
false
DFS is the slowest (240ms) method, BFS methods seems are all same ( all about 150ms and faster than 80+%).\nI think from short length can accelerate which means bfs with level should be fastest, but 4 bfs methods do not have much difference. But anyway, bfs without short length begining is slow just like dfs which begi...
21
0
['Depth-First Search', 'Breadth-First Search', 'Recursion', 'Python']
3
subtree-of-another-tree
50.1 (Approach 1) | O(n.m)✅ | Python & C++(Step by step explanation)✅
501-approach-1-onm-python-cstep-by-step-u2f41
Intuition\nThe problem asks us to determine if a given binary tree subRoot is a subtree of another binary tree root. Our intuition here is to perform a depth-fi
monster0Freason
NORMAL
2024-02-06T18:06:12.946088+00:00
2024-02-06T18:06:12.946117+00:00
3,447
false
# Intuition\nThe problem asks us to determine if a given binary tree `subRoot` is a subtree of another binary tree `root`. Our intuition here is to perform a depth-first search (DFS) traversal of the `root` tree. At each node, we check if the subtree rooted at this node matches the given subtree `subRoot`. \n\n# Approa...
19
0
['Tree', 'Depth-First Search', 'Binary Tree', 'C++', 'Python3']
2
subtree-of-another-tree
✅ C++ | Using one of the previous solution
c-using-one-of-the-previous-solution-by-idai6
\n# Code\nC++\nclass Solution {\npublic:\n bool isSameTree(TreeNode* p, TreeNode* q) {\n if (!p && !q) return true;\n if (!p || !q) return fals
kosant
NORMAL
2023-05-06T08:38:16.472276+00:00
2023-05-06T08:38:16.472309+00:00
4,725
false
\n# Code\n```C++\nclass Solution {\npublic:\n bool isSameTree(TreeNode* p, TreeNode* q) {\n if (!p && !q) return true;\n if (!p || !q) return false;\n if (p->val != q->val) return false;\n\n return isSameTree(p->left, q->left) && isSameTree(p->right, q->right); \n }\n bool isSubtree...
18
0
['Tree', 'Recursion', 'C', 'Binary Tree', 'C++']
1
subtree-of-another-tree
C++ Easiest Solution | DFS | Easy to understand
c-easiest-solution-dfs-easy-to-understan-mn4p
Code\n\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() :
starazmat
NORMAL
2023-03-09T02:40:20.524696+00:00
2023-03-09T02:40:20.524735+00:00
3,203
false
# Code\n```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
17
0
['Depth-First Search', 'Recursion', 'C++']
2
subtree-of-another-tree
Easy solution to understand
easy-solution-to-understand-by-shahchaya-5ak0
Code\n\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() :
shahchayan9
NORMAL
2024-07-05T16:25:04.800699+00:00
2024-07-05T16:25:04.800738+00:00
2,396
false
# Code\n```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
16
0
['C++']
1
subtree-of-another-tree
Depth-First Search✅ | O( n^2)✅ | C++(Step by step explanation)✅
depth-first-search-o-n2-cstep-by-step-ex-rgtk
Intuition\nThe problem is to determine if one binary tree (subRoot) is a subtree of another binary tree (root). A subtree of a tree T is a tree consisting of a
monster0Freason
NORMAL
2023-11-10T18:29:12.917483+00:00
2023-11-10T18:29:12.917512+00:00
1,799
false
# Intuition\nThe problem is to determine if one binary tree (subRoot) is a subtree of another binary tree (root). A subtree of a tree T is a tree consisting of a node in T and all of its descendants. The intuition is to perform a recursive comparison for each node in the root tree. If any subtree of the root tree match...
15
0
['Tree', 'Depth-First Search', 'Recursion', 'C++']
2
subtree-of-another-tree
Subtree of Another Tree - C++ Solution | O(n) Time Complexity
subtree-of-another-tree-c-solution-on-ti-5o0f
Find the inorder and preorder/postorder string of both the trees and check for the substring\n\nclass Solution {\npublic:\n\t// Inorder Traversal\n void inor
fizaashaikh
NORMAL
2021-09-16T14:28:24.465838+00:00
2021-09-17T04:49:43.504130+00:00
1,283
false
**Find the inorder and preorder/postorder string of both the trees and check for the substring**\n```\nclass Solution {\npublic:\n\t// Inorder Traversal\n void inorder(TreeNode* root, string& str)\n {\n if(!root)\n {\n str += \'$\';\n return;\n }\n \n inord...
15
0
['C']
2
subtree-of-another-tree
C++ Easy to Understand Begginer Friendly 0 ms 100% faster
c-easy-to-understand-begginer-friendly-0-7184
\n\nclass Solution {\npublic:\n bool isIdentical(TreeNode* s, TreeNode* t){\n if(!s || !t)\n return s==t;\n \n return(s->val=
Akshatkamboj37
NORMAL
2021-02-17T07:40:43.967856+00:00
2021-02-17T07:40:43.967890+00:00
3,070
false
```\n\nclass Solution {\npublic:\n bool isIdentical(TreeNode* s, TreeNode* t){\n if(!s || !t)\n return s==t;\n \n return(s->val==t->val && isIdentical(s->left,t->left) && isIdentical(s->right,t->right));\n }\n bool isSubtree(TreeNode* s, TreeNode* t) {\n if(!s) return fal...
15
0
['Recursion', 'C', 'C++']
3
subtree-of-another-tree
AC simply readable Python, 2 solutions
ac-simply-readable-python-2-solutions-by-z9lt
\ndef isSubtree(self, s: TreeNode, t: TreeNode) -> bool:\n\tdef dfs(n, t):\n\t\tif not n and not t: return True\n\t\tif not n or not t: return False\n\t\treturn
lccn345
NORMAL
2020-06-01T03:24:21.576257+00:00
2020-06-01T03:25:57.806682+00:00
3,390
false
```\ndef isSubtree(self, s: TreeNode, t: TreeNode) -> bool:\n\tdef dfs(n, t):\n\t\tif not n and not t: return True\n\t\tif not n or not t: return False\n\t\treturn n.val == t.val and dfs(n.left, t.left) and dfs(n.right, t.right)\n\n\tstack = [s]\n\twhile stack:\n\t\tn = stack.pop()\n\t\tif n.val == t.val and dfs(n, t):...
15
1
['Depth-First Search', 'Recursion', 'Python', 'Python3']
3
subtree-of-another-tree
C++ RECURSION +EXPLANATION
c-recursion-explanation-by-82877harsh-yhzb
\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0)
82877harsh
NORMAL
2022-06-01T06:28:23.267946+00:00
2022-06-01T06:28:23.267976+00:00
1,236
false
```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right...
14
0
['Recursion', 'C']
4
subtree-of-another-tree
Java, traversal, Shortest method, 4 lines
java-traversal-shortest-method-4-lines-b-01eb
\nclass Solution {\n public boolean isSubtree(TreeNode s, TreeNode t) {\n if(s==null) return t == null;\n \treturn isSame(s, t) || isSubtree(s.left
liyuanzhe01
NORMAL
2018-11-02T05:38:53.726590+00:00
2018-11-02T05:38:53.726630+00:00
2,333
false
```\nclass Solution {\n public boolean isSubtree(TreeNode s, TreeNode t) {\n if(s==null) return t == null;\n \treturn isSame(s, t) || isSubtree(s.left,t) || isSubtree(s.right,t);\n }\n\t\n private boolean isSame(TreeNode s, TreeNode t){\n \tif(s==null || t == null) return t == s;\n \treturn s.v...
13
0
[]
5
subtree-of-another-tree
[Python] Beautiful iterative DFS solution with two functions
python-beautiful-iterative-dfs-solution-gf325
\nclass Solution:\n \n def is_identical(self,root1,root2):\n stack = [(root1,root2)]\n \n while stack:\n cur1, cur2 = stac
glebmikhaylov
NORMAL
2022-03-02T05:59:58.514936+00:00
2022-03-02T05:59:58.514978+00:00
917
false
```\nclass Solution:\n \n def is_identical(self,root1,root2):\n stack = [(root1,root2)]\n \n while stack:\n cur1, cur2 = stack.pop()\n if cur1 and cur2:\n if cur1.val != cur2.val:\n return False\n stack.append((cur1.left,c...
12
0
['Stack', 'Depth-First Search', 'Iterator', 'Python']
1
subtree-of-another-tree
C++ | 0ms | Clean Code | DFS
c-0ms-clean-code-dfs-by-dhairyabahl-ohtu
Approach : \n\nI recursively traversed over the tree and then on finding the required node which matched the root of the subtree, I then checked if that subtree
dhairyabahl
NORMAL
2021-06-30T22:55:41.934259+00:00
2021-06-30T22:55:41.934302+00:00
1,755
false
Approach : \n\nI recursively traversed over the tree and then on finding the required node which matched the root of the subtree, I then checked if that subtree is a part of the original tree using a helper function.\n\nNote : There might be a better approach than this. Just to mention I got 0ms and faster than 100% on...
12
1
['Tree', 'Depth-First Search', 'C']
5
subtree-of-another-tree
Python | O(n) | super easy to understand
python-on-super-easy-to-understand-by-1s-9d0a
\nclass Solution:\n def isSubtree(self, s: TreeNode, t: TreeNode) -> bool:\n def hashify(node):\n if not node:\n return None
1sagarshah
NORMAL
2021-03-28T08:04:06.213041+00:00
2021-03-28T08:04:06.213082+00:00
1,133
false
```\nclass Solution:\n def isSubtree(self, s: TreeNode, t: TreeNode) -> bool:\n def hashify(node):\n if not node:\n return None\n key = (node.val, hashify(node.left), hashify(node.right))\n return memo.setdefault(key, len(memo))\n \n memo = {}\n ...
12
2
['Python']
4
subtree-of-another-tree
Python by DFS [w/ Comment]
python-by-dfs-w-comment-by-brianchiang_t-ow96
Python by DFS \n\n---\n\nImplementation:\n\n\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n
brianchiang_tw
NORMAL
2020-08-18T14:06:30.263933+00:00
2020-08-18T14:06:30.263983+00:00
4,234
false
Python by DFS \n\n---\n\n**Implementation**:\n\n```\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\n\nclass Solution:\n def isSubtree(self, s: TreeNode, t: TreeNode) ...
12
1
['Depth-First Search', 'Recursion', 'Python', 'Python3']
4
subtree-of-another-tree
💡JavaScript Solution
javascript-solution-by-aminick-k87m
The idea\n1. Build a recursive function isSame that recursively checks whether two tree are the same\n2. Use pre-order DFS, recursively checks whether t is a su
aminick
NORMAL
2019-12-05T04:29:38.213678+00:00
2019-12-05T04:29:38.213824+00:00
2,546
false
### The idea\n1. Build a recursive function `isSame` that recursively checks whether two tree are the same\n2. Use pre-order DFS, recursively checks whether t is a subtree of the current node\n3. During DFS, if the two root nodes are the same, we use `isSame` to check whether they are the same tree\n``` javascript\nvar...
12
0
['JavaScript']
3
subtree-of-another-tree
Java Solution, postorder traversal without using contains, O(N)
java-solution-postorder-traversal-withou-kpyu
\npublic class Solution {\n public boolean isSubtree(TreeNode s, TreeNode t) {\n String tStr = getPostOrderString(t);\n return validateSubtree(
caroline10
NORMAL
2017-08-02T04:26:09.774000+00:00
2018-09-15T18:50:15.260742+00:00
3,790
false
```\npublic class Solution {\n public boolean isSubtree(TreeNode s, TreeNode t) {\n String tStr = getPostOrderString(t);\n return validateSubtree(s, tStr).find;\n }\n \n private String getPostOrderString(TreeNode root) {\n if(root==null) return "#";\n else {\n return g...
12
0
[]
3
subtree-of-another-tree
Python recursive solution with explanation
python-recursive-solution-with-explanati-n29n
\n# Definition for a binary tree node.\n# class TreeNode(object):\n# def __init__(self, x):\n# self.val = x\n# self.left = None\n# s
gtang31
NORMAL
2017-06-02T03:47:09.403000+00:00
2017-06-02T03:47:09.403000+00:00
4,278
false
```\n# Definition for a binary tree node.\n# class TreeNode(object):\n# def __init__(self, x):\n# self.val = x\n# self.left = None\n# self.right = None\n\nclass Solution(object):\n def isSubtree(self, s, t):\n """\n :type s: TreeNode\n :type t: TreeNode\n :rtyp...
12
0
[]
3
subtree-of-another-tree
Golang 15 lines of solution with explanations
golang-15-lines-of-solution-with-explana-jcf8
Two tree is identical when all their nodes are the same. \n2. We can do a preorder traversal on tree s to check whether the current node\'s value is equal to t\
leontung7
NORMAL
2019-12-03T07:42:29.469906+00:00
2019-12-03T07:42:29.469952+00:00
883
false
1. Two tree is identical when all their nodes are the same. \n2. We can do a preorder traversal on tree `s` to check whether the current node\'s value is equal to `t`\'s root. If it is, then we can check if these two subtrees are same. \n\n```\nfunc isSubtree(s *TreeNode, t *TreeNode) bool {\n\tif s == nil || t == nil ...
11
0
['Recursion', 'Go']
1
subtree-of-another-tree
JavaScript | DFS
javascript-dfs-by-bs4-v1cb
\nconst isSameTree = (a, b)=>{\n if (!a && !b) return true;\n if (a && !b) return false;\n if (b && !a) return false;\n if (a.val === b.val) return
bs4
NORMAL
2022-05-13T21:48:15.911331+00:00
2022-05-13T21:48:15.911372+00:00
1,348
false
```\nconst isSameTree = (a, b)=>{\n if (!a && !b) return true;\n if (a && !b) return false;\n if (b && !a) return false;\n if (a.val === b.val) return isSameTree(a.left, b.left) && isSameTree(a.right, b.right);\n}\n\nvar isSubtree = function(root, subRoot) {\n if (!root) return false;\n if (isSameTree...
10
0
['Depth-First Search', 'JavaScript']
2
subtree-of-another-tree
Python, tree traversal with inline comments
python-tree-traversal-with-inline-commen-kwr0
\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, x):\n# self.val = x\n# self.left = None\n# self.righ
dentedghost
NORMAL
2020-02-09T08:27:18.037823+00:00
2020-02-09T08:27:18.037862+00:00
1,997
false
```\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, x):\n# self.val = x\n# self.left = None\n# self.right = None\n\nclass Solution:\n def isSubtree(self, s: TreeNode, t: TreeNode) -> bool:\n # If t tree is none we instantly have a match\n if t ...
10
0
['Recursion', 'Python3']
4
subtree-of-another-tree
javascript 4 lines,maybe it's cheating
javascript-4-linesmaybe-its-cheating-by-d5rp5
\n/**\n * Definition for a binary tree node.\n * function TreeNode(val) {\n * this.val = val;\n * this.left = this.right = null;\n * }\n */\n/**\n * @pa
xq008
NORMAL
2019-01-02T06:47:56.396957+00:00
2019-01-02T06:47:56.397005+00:00
564
false
```\n/**\n * Definition for a binary tree node.\n * function TreeNode(val) {\n * this.val = val;\n * this.left = this.right = null;\n * }\n */\n/**\n * @param {TreeNode} s\n * @param {TreeNode} t\n * @return {boolean}\n */\nvar isSubtree = function(s, t) {\n if(s!=null && t==null){return false;}\n let str...
10
0
[]
4
subtree-of-another-tree
[VIDEO] Visualization of Recursive Depth-First SEarch
video-visualization-of-recursive-depth-f-rdkz
https://youtu.be/Ja7TdUIW-_Q\n\nBefore solving this problem, you should first solve question #100: Same Tree, because we use the solution from that problem to s
AlgoEngine
NORMAL
2024-08-31T16:35:19.539839+00:00
2024-08-31T16:35:19.539862+00:00
1,892
false
https://youtu.be/Ja7TdUIW-_Q\n\nBefore solving this problem, you should first solve question #100: Same Tree, because we use the solution from that problem to solve this one. The `isSameTree` function is taken directly from that problem - it returns `True` if the two trees are the same tree, and returns `False` otherw...
9
0
['Tree', 'Depth-First Search', 'Recursion', 'Binary Tree', 'Python', 'Python3']
1
subtree-of-another-tree
✔O(N + M) Time [Serialization Approach] 👍|| Simple & Easy || C++
on-m-time-serialization-approach-simple-lq64h
Intuition\n Describe your first thoughts on how to solve this problem. \nBy converting into strings we could compare them easily.\n# Approach\n Describe your ap
DineshGangireddi
NORMAL
2023-07-03T11:36:56.468192+00:00
2023-07-03T11:56:28.853179+00:00
1,537
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nBy converting into strings we could compare them easily.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nSerialization of the TREE.\n# Complexity\n- Time complexity:O(N + M)\n-The time complexity of this solution is ...
9
0
['Depth-First Search', 'C++']
3
subtree-of-another-tree
C implementation easy
c-implementation-easy-by-bindhushreehr-sq54
\nbool equals(struct TreeNode* s, struct TreeNode* t)\n{\n if (s==NULL && t==NULL)\n return true;\n else if (s==NULL || t== NULL)\n return f
bindhushreehr
NORMAL
2020-09-03T14:00:28.679325+00:00
2020-09-03T14:00:28.679379+00:00
486
false
```\nbool equals(struct TreeNode* s, struct TreeNode* t)\n{\n if (s==NULL && t==NULL)\n return true;\n else if (s==NULL || t== NULL)\n return false;\n\n return (s->val == t->val) && equals(s->right,t->right) && equals(s->left,t->left);\n}\n\nbool isSubtree(struct TreeNode* s, struct TreeNode* t){...
9
0
['Recursion', 'C']
1
subtree-of-another-tree
Intuitive - Java Fully Iterative Solution - Accepted
intuitive-java-fully-iterative-solution-7e4bs
I didn\'t see any fully iterative solution for this so posting one. I\'m not a big fan of recursion though mostly it is easier to solve tree problems recursivel
Randy_Marsh
NORMAL
2020-06-01T13:15:55.756324+00:00
2020-06-01T13:16:58.041859+00:00
1,762
false
I didn\'t see any fully iterative solution for this so posting one. I\'m not a big fan of recursion though mostly it is easier to solve tree problems recursively than iteratively. Although it wasn\'t one of those problems. Let me know if it can be made better.\n\n```\n/**\n * Definition for a binary tree node.\n * publ...
9
0
['Iterator', 'Java']
4
subtree-of-another-tree
[C++] [Java] Clean Code - 2 one liner
c-java-clean-code-2-one-liner-by-alexand-rfz6
C++\n\nclass Solution {\npublic:\n bool isSubtree(TreeNode* s, TreeNode* t) {\n return !t || s && (same(s, t) || isSubtree(s->left, t) || isSubtree(s-
alexander
NORMAL
2017-05-07T03:04:27.723000+00:00
2017-05-07T03:04:27.723000+00:00
5,571
false
**C++**\n```\nclass Solution {\npublic:\n bool isSubtree(TreeNode* s, TreeNode* t) {\n return !t || s && (same(s, t) || isSubtree(s->left, t) || isSubtree(s->right, t));\n }\n\nprivate:\n bool isSameTree(TreeNode* s, TreeNode* t) {\n return !s ? !t : t && s->val == t->val && isSameTree(s->left, t...
9
4
[]
5
subtree-of-another-tree
PATTERN || ✅Beats 97 time and space || Recursive DFS iterative BFS and DFS
pattern-beats-97-time-and-space-recursiv-77sp
Intuition\nfor every node check if the root node val equals subRoot node if not the check for left and right nodes, never found then return false.\n\n\n# Comple
Dixon_N
NORMAL
2024-07-09T19:21:51.791665+00:00
2024-07-09T19:21:51.791690+00:00
1,464
false
# Intuition\nfor every node check if the root node val equals subRoot node if not the check for left and right nodes, never found then return false.\n\n\n# Complexity\n\n```\nSolution |\tTime |\tSpace Complexity\n -------------------------------------------\nRecursive DFS |\tO(N * M) |\tO(N)\nIterative BFS |...
7
0
['Java']
5
subtree-of-another-tree
Python | Easy | Tree | Subtree of Another Tree
python-easy-tree-subtree-of-another-tree-f88k
\nsee the Successfully Accepted Submission\nPython\nclass Solution(object):\n def isSubtree(self, root, subRoot):\n """\n :type root: TreeNode\
Khosiyat
NORMAL
2023-10-09T16:13:46.324029+00:00
2023-10-09T16:13:46.324047+00:00
993
false
\n[see the Successfully Accepted Submission](https://leetcode.com/submissions/detail/937413302/)\n```Python\nclass Solution(object):\n def isSubtree(self, root, subRoot):\n """\n :type root: TreeNode\n :type subRoot: TreeNode\n :rtype: bool\n """ \n if root is None and subRo...
7
0
['Tree', 'Python']
1
subtree-of-another-tree
java
java-by-mikhmanoff-p28x
Code\n\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * int val;\n * TreeNode left;\n * TreeNode right;\n * TreeNode(
mikhmanoff
NORMAL
2023-05-12T16:39:15.839365+00:00
2023-05-12T16:39:15.839408+00:00
1,798
false
# Code\n```\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * int val;\n * TreeNode left;\n * TreeNode right;\n * TreeNode() {}\n * TreeNode(int val) { this.val = val; }\n * TreeNode(int val, TreeNode left, TreeNode right) {\n * this.val = val;\n * this....
7
0
['Python', 'C++', 'Java']
1
subtree-of-another-tree
Simple recursvie java solution with explaination o(nm)
simple-recursvie-java-solution-with-expl-b77b
\n# Approach\nTry normal tree traversal and check if at any point root.val==subRoot.val. if yes, just start comparing every node. if entire tree same return tru
kushguptacse
NORMAL
2022-12-26T05:50:16.604077+00:00
2022-12-26T05:50:57.753507+00:00
1,694
false
\n# Approach\nTry normal tree traversal and check if at any point root.val==subRoot.val. if yes, just start comparing every node. if entire tree same return true else false.\n\n# Code\n```\nclass Solution {\n public boolean isSubtree(TreeNode root, TreeNode subRoot) {\n //if either tree exhausted. answer not ...
7
0
['Tree', 'Recursion', 'Binary Tree', 'Java']
1
subtree-of-another-tree
[ JAVASCRIPT] Easy recursive solution + explanation (using helper function)
javascript-easy-recursive-solution-expla-03z4
\nvar isSubtree = function (root, subRoot) {\n if (root == null) return false;\n if (isSameTree(root, subRoot) || subRoot == null) return true;\n return isSu
sararita28
NORMAL
2022-02-19T13:59:26.723491+00:00
2022-03-20T12:26:40.413810+00:00
813
false
```\nvar isSubtree = function (root, subRoot) {\n if (root == null) return false;\n if (isSameTree(root, subRoot) || subRoot == null) return true;\n return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);\n};\n\n const isSameTree = (tree1, tree2) => {\n if (tree1 == null && tree2 == null) return ...
7
0
['Recursion', 'JavaScript']
3
subtree-of-another-tree
[C++] Simple DFS Solution Explained, ~100% Time, 90% Space
c-simple-dfs-solution-explained-100-time-b1b8
A fine little tree problem, which can be conveniently split into two:\n finding a node which might come from a similar tree structure (ie: arriving to a node wi
Ajna2
NORMAL
2022-02-13T22:59:19.156056+00:00
2022-02-13T22:59:19.156088+00:00
1,052
false
A fine little tree problem, which can be conveniently split into two:\n* finding a node which might come from a similar tree structure (ie: arriving to a node with the same value as `subRoot->val`);\n* starting from there to check if we have the same tree or not.\n\nOur main function (`isSubTree`) will do precisely tha...
7
0
['Depth-First Search', 'Graph', 'C', 'C++']
2
subtree-of-another-tree
Easy Solution must try**
easy-solution-must-try-by-himanshubhoir-rtqw
\nvar isSubtree = function(root, subRoot) {\n if(root == null) return false;\n if(same(root,subRoot)) return true;\n return isSubtree(root.left,sub
HimanshuBhoir
NORMAL
2021-12-21T03:50:14.414312+00:00
2021-12-21T03:50:14.414349+00:00
562
false
```\nvar isSubtree = function(root, subRoot) {\n if(root == null) return false;\n if(same(root,subRoot)) return true;\n return isSubtree(root.left,subRoot) || isSubtree(root.right,subRoot);\n};\n\nfunction same(root,subRoot){\n if(root == null && subRoot == null) return true;\n if(root == null || sub...
7
0
['JavaScript']
1
subtree-of-another-tree
Python, Tree Hash 🌲#️⃣
python-tree-hash-by-yasir991925-s2s2
Build a tree hash\nThe main idea is to convert tree to a string\nThen the question becomes finding substring == subRoot tree hash ( which is a string now ) \np
yasir991925
NORMAL
2021-05-09T12:22:25.905600+00:00
2021-07-02T15:58:24.449990+00:00
1,209
false
Build a tree hash\nThe main idea is to convert **tree to a string**\nThen the question becomes finding substring == **subRoot tree hash** ( which is a string now ) \n```python\nclass Solution:\n def isSubtree(self, root: TreeNode, subRoot: TreeNode) -> bool:\n\n def f(root):\n if not root:\n ...
7
0
['Python', 'Python3']
1
subtree-of-another-tree
C++ || Better and Concise Solution || Easy to understand
c-better-and-concise-solution-easy-to-un-zolg
Runtime: 32 ms, faster than 96.47% of C++ online submissions for Subtree of Another Tree.\nMemory Usage: 29.4 MB, less than 53.93% of C++ online submissions for
anonymous_kumar
NORMAL
2020-08-01T15:31:28.181210+00:00
2020-08-01T15:34:51.731106+00:00
1,441
false
***Runtime: 32 ms, faster than 96.47% of C++ online submissions for Subtree of Another Tree.\nMemory Usage: 29.4 MB, less than 53.93% of C++ online submissions for Subtree of Another Tree.***\n```\nclass Solution {\npublic:\n bool isSubtree(TreeNode* s, TreeNode* t) {\n if(!s || !t){\n return s == ...
7
1
['C', 'C++']
1
subtree-of-another-tree
Python version
python-version-by-zjlvmiao-80zd
\n def isSubtree(self, s, t):\n if not s:\n return False\n if self.isSame(s,t):\n return True\n return self.isSubt
zjlvmiao
NORMAL
2017-05-14T18:31:06.196000+00:00
2017-05-14T18:31:06.196000+00:00
865
false
\n def isSubtree(self, s, t):\n if not s:\n return False\n if self.isSame(s,t):\n return True\n return self.isSubtree(s.left,t) or self.isSubtree(s.right,t)\n \n def isSame(self,s,t):\n if not s and not t:\n return True\n if not s or not t...
7
0
[]
5
subtree-of-another-tree
Easy Java solution | DFS
easy-java-solution-dfs-by-aishi535-hdwl
Hope this helps.\nDo Upvote if you like it !!\n\nThanks :)\n\n# Code\n\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * int val;\
aishi535
NORMAL
2023-01-21T10:06:52.873655+00:00
2023-01-21T10:06:52.873689+00:00
1,744
false
Hope this helps.\nDo Upvote if you like it !!\n\nThanks :)\n\n# Code\n```\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * int val;\n * TreeNode left;\n * TreeNode right;\n * TreeNode() {}\n * TreeNode(int val) { this.val = val; }\n * TreeNode(int val, TreeNode left, T...
6
0
['Tree', 'Depth-First Search', 'Binary Tree', 'Java']
1
subtree-of-another-tree
Java
java-by-niyazjava-yp8k
If you like it pls upvote\n\n\n public boolean isSubtree(TreeNode root, TreeNode subRoot) {\n if (root == null) return false;\n if (dfs(root, s
NiyazJava
NORMAL
2022-11-21T00:47:14.528865+00:00
2022-11-21T00:49:56.039389+00:00
1,062
false
If you like it pls upvote\n```\n\n public boolean isSubtree(TreeNode root, TreeNode subRoot) {\n if (root == null) return false;\n if (dfs(root, subRoot)) return true;\n return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);\n }\n\n public boolean dfs(TreeNode root, TreeNo...
6
0
['Depth-First Search', 'Java']
0
subtree-of-another-tree
Java Easy to Understand Recursive Solution || Faster than 96%
java-easy-to-understand-recursive-soluti-58ys
\nclass Solution {\n public boolean isSubtree(TreeNode root, TreeNode subRoot) {\n if (subRoot == null) return true;\n if (root == null) return
starman214
NORMAL
2022-09-08T11:13:49.213547+00:00
2022-09-08T11:13:49.213594+00:00
768
false
```\nclass Solution {\n public boolean isSubtree(TreeNode root, TreeNode subRoot) {\n if (subRoot == null) return true;\n if (root == null) return false;\n if (root.val == subRoot.val) {\n if (helper(root, subRoot)) return true;\n }\n return isSubtree(root.left, subRoot)...
6
0
['Recursion', 'Binary Tree', 'Java']
1
subtree-of-another-tree
Simple & Easy Java Solution
simple-easy-java-solution-by-himanshuram-0gcw
Upvote if you Got It \uD83D\uDE42\nclass Solution {\n\n public boolean isSubtree(TreeNode root, TreeNode subRoot) {\n\t\n if(root==null) return false;
himanshuramranjan
NORMAL
2022-01-04T04:20:38.850909+00:00
2022-01-04T05:07:16.206343+00:00
852
false
**Upvote if you Got It** \uD83D\uDE42\nclass Solution {\n\n public boolean isSubtree(TreeNode root, TreeNode subRoot) {\n\t\n if(root==null) return false;\n if(subRoot==null) return true;\n return helper(root,subRoot) || isSubtree(root.left,subRoot) || isSubtree(root.right,subRoot);\n }\n ...
6
1
['Recursion', 'Java']
2
subtree-of-another-tree
Python3 recursive solution
python3-recursive-solution-by-eklavyajos-yhrf
\nclass Solution:\n def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool:\n if not root:\n return\n de
EklavyaJoshi
NORMAL
2021-10-16T06:46:01.524225+00:00
2021-10-16T06:46:01.524270+00:00
643
false
```\nclass Solution:\n def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool:\n if not root:\n return\n def check(r,s):\n if not s and not r:\n return True\n if not s or not r:\n return False\n if r.v...
6
1
['Recursion', 'Python3']
0
subtree-of-another-tree
Javascript - easy explanation
javascript-easy-explanation-by-achandel-cvm0
\nvar isSubtree = function(s, t) {\n // helper fn to check if both tree are same\n const isSameTree = (t1, t2) => {\n if(!t1 && !t2) return true;\n
achandel
NORMAL
2020-06-11T15:50:57.847465+00:00
2020-06-11T15:50:57.847514+00:00
970
false
```\nvar isSubtree = function(s, t) {\n // helper fn to check if both tree are same\n const isSameTree = (t1, t2) => {\n if(!t1 && !t2) return true;\n if(!t1 || !t2) return false;\n if(t1.val !== t2.val) return false;\n \n return isSameTree(t1.left, t2.left) && isSameTree(t1.right, t2...
6
0
['Recursion', 'JavaScript']
1
subtree-of-another-tree
6 line Java Solution that beats 100% both space and runtime
6-line-java-solution-that-beats-100-both-tszc
\n public boolean isSubtree(TreeNode s, TreeNode t) {\n return isSubtreeHelper(s, t, false); \n }\n \n boolean isSubtreeHelper(TreeNode s, Tr
deleted_user
NORMAL
2019-09-09T16:04:27.638108+00:00
2019-09-20T22:59:42.866918+00:00
418
false
```\n public boolean isSubtree(TreeNode s, TreeNode t) {\n return isSubtreeHelper(s, t, false); \n }\n \n boolean isSubtreeHelper(TreeNode s, TreeNode t, boolean started) {\n if (s == null && t == null) return true;\n if (s == null || t == null) return false;\n if (s.val == t.val...
6
0
[]
2
subtree-of-another-tree
Python solution
python-solution-by-rogerfederer-akkc
\nclass Solution(object):\n def isSameTree(self, s, t):\n if not s and not t:\n return True\n\n if not s and t or s and not t:\n
rogerfederer
NORMAL
2018-06-26T11:10:21.729018+00:00
2018-06-26T11:10:21.729018+00:00
1,782
false
```\nclass Solution(object):\n def isSameTree(self, s, t):\n if not s and not t:\n return True\n\n if not s and t or s and not t:\n return False\n\n if s.val != t.val:\n return False\n\n return self.isSameTree(s.left, t.left) and self.isSameTree(s.right, t...
6
1
[]
0
subtree-of-another-tree
SIMPLE RECURSIVE C++ COMMENTED SOLUTION
simple-recursive-c-commented-solution-by-zw9f
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
Jeffrin2005
NORMAL
2024-09-14T03:12:57.679146+00:00
2024-09-14T03:12:57.679173+00:00
533
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
5
0
['C++']
0
subtree-of-another-tree
Blind 75 - Beats 90.07% [30/75]
blind-75-beats-9007-3075-by-rahulbnair-0ccc
Intuition\nFind the root node of the subRoot in the main tree. Once found, check whether they are the same tree. prerequsite: Leetcode 100. Same tree\n\n# Appro
rahulbnair
NORMAL
2024-04-08T15:25:46.641988+00:00
2024-04-15T08:47:19.374987+00:00
1,846
false
# Intuition\n*Find the root node of the subRoot in the main tree. Once found, check whether they are the same tree. prerequsite: Leetcode 100. Same tree*\n\n# Approach\nHere\'s a breakdown of the code:\n\n1. **Helper Method (dfs_sameTree)**:\n - This method recursively checks if two trees (`node1` and `node2`) are th...
5
0
['Python3']
0
subtree-of-another-tree
🔥Easiest Solution out there in 5 Lines 💯
easiest-solution-out-there-in-5-lines-by-mowq
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
No_one_can_stop_me
NORMAL
2023-03-15T15:41:59.359428+00:00
2023-03-15T15:41:59.359465+00:00
1,017
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
5
0
['Java']
2
subtree-of-another-tree
572: Solution with step by step explanation
572-solution-with-step-by-step-explanati-wj1n
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n1. Define a function checkEqual to check if two trees are equal in value
Marlen09
NORMAL
2023-03-15T05:30:46.187100+00:00
2023-03-15T05:30:46.187132+00:00
2,839
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Define a function checkEqual to check if two trees are equal in value and structure. The function takes two tree nodes root1 and root2 as input and returns a boolean value.\n\n2. Within the checkEqual function, check if b...
5
0
['Tree', 'Depth-First Search', 'String Matching', 'Python', 'Python3']
0
subtree-of-another-tree
JAVA Solution || Steps,TC and SC explained. Code is kept simple.
java-solution-stepstc-and-sc-explained-c-m0q8
```\nclass Solution {\n public boolean isSubtree(TreeNode root, TreeNode subRoot) {\n //return true is subroot is null\n if(subRoot == null) {\
Shaikh_Ovais
NORMAL
2022-07-03T06:52:32.478420+00:00
2022-07-03T10:28:49.511009+00:00
493
false
```\nclass Solution {\n public boolean isSubtree(TreeNode root, TreeNode subRoot) {\n //return true is subroot is null\n if(subRoot == null) {\n return true;\n }\n \n //return false if subroot is not null but root is null\n if(root == null) {\n return f...
5
0
['Recursion', 'Java']
0
subtree-of-another-tree
Easy-understanding Python code with detailed explanation for beginner
easy-understanding-python-code-with-deta-8ukz
We can use two recursive functions to solve this problem. \n1. First recursive function to check whether two tree is same. If cur value is the same, then check
jlu56
NORMAL
2022-03-21T03:03:08.225516+00:00
2022-03-21T03:03:08.225566+00:00
1,059
false
We can use two recursive functions to solve this problem. \n1. First recursive function to check whether two tree is same. If cur value is the same, then check the right node and left node. (logic is and)\n2. Second recursive function to check each subtree in the original tree using the first function. If current nod...
5
0
['Recursion', 'Python', 'Python3']
2
subtree-of-another-tree
Recursive Solution Accepted | Need Help for KMP algorithm
recursive-solution-accepted-need-help-fo-9wrq
Working Recursion Solution\n\nclass Solution {\npublic:\n bool isSubtree(TreeNode* root, TreeNode* subRoot , bool check=true) {\n if(root==NULL and su
abhinandanmishra1
NORMAL
2022-03-03T18:12:45.812486+00:00
2022-03-03T18:12:45.812532+00:00
174
false
**Working Recursion Solution**\n```\nclass Solution {\npublic:\n bool isSubtree(TreeNode* root, TreeNode* subRoot , bool check=true) {\n if(root==NULL and subRoot==NULL) return true;\n if(!root or !subRoot) return false;\n bool ans=false;\n if(root->val == subRoot->val){\n ans=...
5
0
['Recursion']
1
subtree-of-another-tree
Python 90.26% solution | O(mn) | O(m+n) solutions
python-9026-solution-omn-omn-solutions-b-dbbh
Naive Solution: Traverse the tree and check if the node values match and then check if both the subTrees are same. \nTime Complexity: O(mn)\n\n\n\tclass Solutio
findkushalmukherjee
NORMAL
2022-01-12T10:21:28.481388+00:00
2022-01-12T10:22:17.740970+00:00
381
false
Naive Solution: Traverse the tree and check if the node values match and then check if both the subTrees are same. \nTime Complexity: O(mn)\n\n```\n\tclass Solution(object):\n\t\tdef isSubtree(self, root, subRoot):\n\n\t\t\tif root == None:\n\t\t\t\treturn False\n\n\t\t\tif root.val == subRoot.val and self.isSame(root,...
5
0
['Hash Table', 'Binary Tree', 'Hash Function']
0
subtree-of-another-tree
C++ commented solution
c-commented-solution-by-shubhamkumar0-twom
\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0)
shubhamkumar0
NORMAL
2021-09-03T22:41:41.155191+00:00
2021-09-03T22:41:41.155220+00:00
444
false
```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right...
5
1
['C']
0
subtree-of-another-tree
JAVA Easy to Understand Recursive Approach
java-easy-to-understand-recursive-approa-eiov
\nclass Solution {\n public boolean isSubtree(TreeNode root, TreeNode subRoot) {\n if(root==null){\n return false;\n }\n if(a
prachiigoyal
NORMAL
2021-07-16T20:11:34.195911+00:00
2021-07-16T20:11:34.195958+00:00
722
false
```\nclass Solution {\n public boolean isSubtree(TreeNode root, TreeNode subRoot) {\n if(root==null){\n return false;\n }\n if(areSame(root,subRoot)){\n return true;\n }else{\n return isSubtree(root.left,subRoot) || isSubtree(root.right,subRoot);\n ...
5
1
['Recursion', 'Java']
0