Skip to content

Commit

Permalink
update cheatsheet
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Mar 6, 2024
1 parent aaf01c2 commit 777a05f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
20240306:
20240305: 100,102,104,105(again)
20240304: 73,76,79(again),91,25
20240303: 55(again),56,62,70
Expand Down
46 changes: 46 additions & 0 deletions doc/cheatsheet/binary_tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,52 @@

### 0-2) Pattern

### 0-2-1) Construct Binary Tree from Preorder and Inorder Traversal

- LC 105
- https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Recursion/construct-binary-tree-from-preorder-and-inorder-traversal.py

- NOTE !!!
- Binary tree is `symmetric`, so the distance(left-sub-tree, root), distance(right-sub-tree, root) is the same
- and the distance above is "idx"
- `root` always at first element when Preorder

- Steps
- step 1) get root val in Preorder
- step 2) get root idx in Inorder
- step 3) split sub left tree, sub right tree in Preorder, Inorder via root idx


```
Preorder :
root ---- left - right
<--> <-->
idx idx
Inorder :
left ---- root - right
<--> <-->
idx idx
```

so we can use below go represent left, right sub tree in Preorder and Inorder

```python
# python

# Preorder:
# left sub tree : preorder[1 : index + 1]
# right sub tree : preorder[index + 1 : ]

# Inorder
# left sub tree : inorder[ : index]
# right sub tree : inorder[index + 1 :]
```


## 1) General form

### 1-1) Basic OP
Expand Down

0 comments on commit 777a05f

Please sign in to comment.