Skip to content

Commit

Permalink
solve two problems BST
Browse files Browse the repository at this point in the history
  • Loading branch information
nghoanglong committed Aug 14, 2021
1 parent 73de7fd commit 57c4825
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
47 changes: 45 additions & 2 deletions 07 BINARY SEARCH TREE/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,38 +126,75 @@ Time complexity: O(n), n = number of node

traversal: LEFT -> ROOT -> RIGHT
```python
# Space Complexity: O(1), print out the result line by line

def inorder_traverse(root):
if root is not None:
inorder_traverse(root.left)
print(root.value)
inorder_traverse(root.right)
```

```python
# Space Complexity: O(n), return the result in the array

def inorder_traverse(root):
if root is None: return []
left = self.inorderTraversal(root.left)
right = self.inorderTraversal(root.right)
return left + [root.val] + right
```
### Preorder traversal

Time complexity: O(n), n = number of node

traversal: ROOT -> LEFT -> RIGHT
```python
# Space Complexity: O(1), print out the result line by line

def preorder_traverse(root):
if root is not None:
print(root.value)
preorder_traverse(root.left)
preorder_traverse(root.right)
```

```python
# Space Complexity: O(n), return the result in the array

def preorder_traverse(root):
if root is None: return []
left = self.inorderTraversal(root.left)
right = self.inorderTraversal(root.right)
return [root.val] + left + right
```


### Postorder traversal

Time complexity: O(n), n = number of node

traversal: LEFT -> RIGHT -> ROOT
```python
# Space Complexity: O(1), print out the result line by line

def postorder_traverse(root):
if root is not None:
preorder_traverse(root.left)
preorder_traverse(root.right)
print(root.value)
```

```python
# Space Complexity: O(n), return the result in the array

def postorder_traverse(root):
if root is None: return []
left = self.inorderTraversal(root.left)
right = self.inorderTraversal(root.right)
return left + right + [root.val]
```

## Height of BST

Time complexity: O(n)
Expand All @@ -174,7 +211,7 @@ def height_tree(root):
else:
left_h = height_tree(root.left)
right_h = height_tree(root.right)
max_h = left_h < right_h and right_h or left_h
max_h = max(left_h, right_h)
return max_h + 1
```
```python
Expand All @@ -185,7 +222,7 @@ def height_tree(root):
else:
left_h = height_tree(root.left)
right_h = height_tree(root.right)
max_h = left_h < right_h and right_h or left_h
max_h = max(left_h, right_h)
return max_h + 1
```

Expand All @@ -202,3 +239,9 @@ def delete_tree(root):
delete_tree(root.right)
del root
```

## Practice

[Binary Tree Inorder Traversal](https://github.com/nghoanglong/DataStructures-Algorithms-CheatSheet/blob/master/06%20BINARY%20SEARCH/search_in_rst.py)

[Maximum Depth of Binary Tree](https://github.com/nghoanglong/DataStructures-Algorithms-CheatSheet/blob/master/06%20BINARY%20SEARCH/search_ins_position.py)
21 changes: 21 additions & 0 deletions 07 BINARY SEARCH TREE/inorder_traversal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Link: https://leetcode.com/problems/binary-tree-inorder-traversal/
# Site: LeetCode
# Name: Binary Tree Inorder Traversal
# Easy
# ======================================================

# Time Complexity: O(n)
# Space Complexity: O(n)

# 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 = right
class Solution:
def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
if root is None: return []
left = self.inorderTraversal(root.left)
right = self.inorderTraversal(root.right)
return left + [root.val] + right
24 changes: 24 additions & 0 deletions 07 BINARY SEARCH TREE/maximum_depth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Link: https://leetcode.com/problems/maximum-depth-of-binary-tree/
# Site: LeetCode
# Name: Maximum Depth of Binary Tree
# Easy
# ======================================================

# Time Complexity: O(n)
# Space Complexity: O(1)

# 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 = right
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if root is None: return 0
else:
left_height = self.maxDepth(root.left)
right_height = self.maxDepth(root.right)
max_height = max(left_height, right_height)
return 1 + max_height

0 comments on commit 57c4825

Please sign in to comment.