Skip to content

Commit

Permalink
fix dfs, bfs cheatsheet, add 652 py example
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Oct 12, 2023
1 parent 1150572 commit a628ecb
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 38 deletions.
10 changes: 7 additions & 3 deletions doc/cheatsheet/bfs.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,14 @@ def bfs(root):
return
q =[]
q.append(root)
while root:
while q:
for i in range(len(q)):
### NOTE : we need to pop the 1st element (idx = 0), BUT NOT THE LAST element
# -> default is pop last element (e.g. q.pop() equals q.pop(-1))
"""
### NOTE :
we need to pop the 1st element (idx = 0), BUT NOT THE LAST element
-> default is pop last element (e.g. q.pop() equals q.pop(-1))
"""
root = q.pop(0)
# do sth
if root.left:
Expand Down
78 changes: 43 additions & 35 deletions doc/cheatsheet/dfs.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,39 @@

### 0-2) Pattern

#### 0-2-1) General form

```python
# form I
def dfs(root):

# if root, do sth
if root:
# do sth, pre-order (root->left->right) in this code

# if not root, do NOTHING

# if root.left exist
if root.left:
dfs(root.left)
# if root.right exist
if root.right:
dfs(root.right)

# form II
def dfs(root):

# if root, do sth
if root:
# do sth, pre-order (root->left->right) in this code

# if not root, nothing to do

dfs(root.left)
dfs(root.right)
```

#### 0-2-2) Basic Tricks

- Important !!!!
```java
Expand All @@ -51,6 +84,8 @@ private TreeNode _dfs(TreeNode node){
}
```

- Tree transversal (DFS)

```python
# python
# form I : tree transversal
Expand All @@ -68,6 +103,8 @@ def dfs(root, target):
# do sth
```

- Tree value moddify (DFS)

```python
# form II : modify values in tree

Expand Down Expand Up @@ -109,11 +146,14 @@ class Solution(object):
"""
if not root:
return TreeNode(val)

if root.val < val:
root.right = self.insertIntoBST(root.right, val);
root.right = self.insertIntoBST(root.right, val)

elif root.val > val:
root.left = self.insertIntoBST(root.left, val);
return(root)
root.left = self.insertIntoBST(root.left, val)

return root
```

```python
Expand Down Expand Up @@ -168,38 +208,6 @@ test()
print (z)
```

## 1) General form
```python
# form I
def dfs(root):

# if root, do sth
if root:
# do sth

# if not root, nothing to do

# if root.left exist
if root.left:
dfs(root.left)
# if root.right exist
if root.right:
dfs(root.right)

# form II
def dfs(root):

# if root, do sth
if root:
# do sth

# if not root, nothing to do

if root.left:
dfs(root.left)
dfs(root.right)
```

### 1-1) Basic OP

#### 1-1-1) Add 1 to all node.value in Binary tree?
Expand Down
15 changes: 15 additions & 0 deletions leetcode_python/Tree/find-duplicate-subtrees.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,27 @@
#
# In [36]: m
# Out[36]: defaultdict(int, {0: 0, 1: 1, 2: 1})
#
# Example 1 : root = [2,1,1]
# output :
# >>> m = defaultdict(<type 'int'>, {'2-1-#-#-1-#-#': 1, '1-#-#': 2})
#
# Example 2 : root = [2,2,2,3,null,3,null]
# output :
# >>> m = defaultdict(<type 'int'>, {'2-2-3-#-#-#-2-3-#-#-#': 1, '2-3-#-#-#': 2, '3-#-#': 2})
#
# Example 3 : root = [1,2,3,4,null,2,4,null,null,4]
# output :
# >>> m = defaultdict(<type 'int'>, {'4-#-#': 3, '1-2-4-#-#-#-3-2-4-#-#-#-4-#-#': 1, '2-4-#-#-#': 2, '3-2-4-#-#-#-4-#-#': 1})
#
#
import collections
class Solution(object):
def findDuplicateSubtrees(self, root):
res = []
m = collections.defaultdict(int)
self.dfs(root, m, res)
#print(">>> m = " + str(m))
return res

def dfs(self, root, m, res):
Expand Down

0 comments on commit a628ecb

Please sign in to comment.