diff --git a/doc/cheatsheet/bfs.md b/doc/cheatsheet/bfs.md index 27fe218c..45175673 100644 --- a/doc/cheatsheet/bfs.md +++ b/doc/cheatsheet/bfs.md @@ -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: diff --git a/doc/cheatsheet/dfs.md b/doc/cheatsheet/dfs.md index 57103440..8b9e58ad 100644 --- a/doc/cheatsheet/dfs.md +++ b/doc/cheatsheet/dfs.md @@ -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 @@ -51,6 +84,8 @@ private TreeNode _dfs(TreeNode node){ } ``` +- Tree transversal (DFS) + ```python # python # form I : tree transversal @@ -68,6 +103,8 @@ def dfs(root, target): # do sth ``` +- Tree value moddify (DFS) + ```python # form II : modify values in tree @@ -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 @@ -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? diff --git a/leetcode_python/Tree/find-duplicate-subtrees.py b/leetcode_python/Tree/find-duplicate-subtrees.py index 092292a3..6362f3c5 100644 --- a/leetcode_python/Tree/find-duplicate-subtrees.py +++ b/leetcode_python/Tree/find-duplicate-subtrees.py @@ -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(, {'2-1-#-#-1-#-#': 1, '1-#-#': 2}) +# +# Example 2 : root = [2,2,2,3,null,3,null] +# output : +# >>> m = defaultdict(, {'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(, {'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):