Trees Lecture - G5 -II - No Code
Trees Lecture - G5 -II - No Code
Part II
Simulation
Given the root of a binary tree, return its maximum depth. A binary tree maximum
depth is the number of nodes along the longest path from the root node down to
the farthest leaf node.
Example 1:
3
Input: root = [3,9,20,null,null,15,7]
Output: 3
9 20 Example 2:
Input: root = [1,null,2]
Output: 2
15 7
2
Simulation - Solution
● What if the tree is empty?
Answer: max_depth = 0
3
Simulation - Solution
1 + max(max_depth(root.left), max_depth(root.right))
9 20
15 7
4
Simulation - Solution
1 + max(max_depth(root.left), max_depth(root.right))
1
9 20
15 7
5
Simulation - Solution
1 + max(1, max_depth(root.right))
9 20
15 7
6
Simulation - Solution
1 + max(1, max_depth(root.right))
1 + max(max_depth(root.left),
max_depth(root.right))
9 20
15 7
7
Simulation - Solution
1 + max(1, max_depth(root.right))
1 + max(max_depth(root.left),
max_depth(root.right))
9 20
15 7
8
Simulation - Solution
1 + max(1, max_depth(root.right))
1 + max(1,max_depth(root.right))
9 20
15 7
9
Simulation - Solution
1 + max(1, max_depth(root.right))
1 + max(1,max_depth(root.right))
9 20
15 7
10
Simulation - Solution
1 + max(1, max_depth(root.right))
1 + max(1, 1) = 2
9 20
15 7
11
Simulation - Solution
1 + max(1,2) = 3
9 20
15 7
12
Question
Insert into a Binary
Search Tree
13
Basic Operation on Trees
14
Operation - Searching
● The algorithm depends on the property of BST that if each left subtree
has values below parent and each right subtree has values above the
parent.
● If the value is below the parent, we can say for sure that the value is
not in the right subtree; we need to only search in the left subtree
● If the value is above the parent, we can say for sure that the value is
not in the left subtree; we need to only search in the right subtree.
● Let us try to visualize this with a diagram searching for 4 in the tree:
15
Operation - Searching
8
8
3 10
1 6 14
4 7
16
Operation - Searching
8
3
3 10
8
1 6 14
4 7
17
Operation - Searching
8
6
3 10
3
1 6 14
8
4 7
18
Operation - Searching
8
4
3 10
6
1 6 14
3
8
4 7
19
Operation - Insertion
try to maintain the BST rule that the left subtree is lesser than root and the
right subtree is larger than root.
● We keep going to either right subtree or left subtree depending on the value
and when we reach a point left or right subtree is null, we put the new node
there.
20
Operation - Insertion
Insert 5 in to the BST
8
3 10
1 6
14
4 7
21
Operation - Insertion
Insert 5 in to the BST
8
3 10
1 6
14
4 7
22
Operation - Insertion
Insert 5 in to the BST
8
3 10
1 6
14
4 7
23
Operation - Insertion
Insert 5 in to the BST
8
3 10
1 6
14
4 7
24
Operation - Insertion
Insert 5 in to the BST
8
3 10
1 6
14
4 7
5
25
Practice Problem
Delete Node in a BST
26
Operation - Deletion
● There are three cases for deleting a node from a binary search tree.
Case One: In the first case, the node to be deleted is the leaf node. In such a case, simply
delete the node from the tree. 4 is to be deleted.
3 10
1 6
14
4 7
27
Operation - Deletion
8
3 10
1 6
14
4 7
28
Operation - Deletion
Case Two: In the second case, the node to be deleted lies has a single child node.
In such a case follow the steps below:
3 10
1 6
14
4 7
29
Operation - Deletion
8
3 10
1 6
14
4 7
30
Operation - Deletion
Case Three: The node to be deleted has two children. In such a case follow the steps
below:
31
Operation - Deletion
8 Inorder traversal: 1 3 4 6 7 8 10 14
3 10
1 6
14
4 7
32
Operation - Deletion
8 Inorder traversal: 1 3 4 6 7 8 10 14
3 10
1 6
14
4 7
33
Operation - Deletion
8 Inorder traversal: 1 4 6 7 8 10 14
4 10
1 6
14
3 7
34
Time and Space Complexity Analysis
Binary Tree Binary Search Tree
● Traversing ● Traversing
○ Time = ? ○ Time = ?
● Searching ● Searching
○ Time = ? ○ Time = ?
● Insertion ● Insertion
○ Time = ? ○ Time = ?
● Deletion ● Deletion
○ Time = ? ○ Time = ?
● Space = ? ● Space = ?
35
Time and Space Complexity Analysis
Binary Tree Binary Search Tree
● Traversing ● Traversing
○ Time = O(n) ○ Time = O(n).
● Searching ● Searching
○ Time = O(n) ○ Time = O(h) where h is the height of BST
● Insertion ● Insertion
○ Time = O(n) ○ Time = O(h)
● Deletion ● Deletion
○ Time = O(n) ○ Time = O(h)
● Space = O(n) … Why? ● Space = O(n)
36
Common Pitfalls
37
Applications of Trees
● Representation structure in File Explorer. (Folders and Subfolders) uses N-ary Tree.
● Auto-suggestions when you google something using Trie.
● Used in decision-based machine learning algorithms.
● Tree forms the backbone of other complex data structures like heap, priority queue,
spanning tree, etc.
● A binary tree is used in database indexing to store and retrieve data in an efficient
manner.
● Binary Search Trees (BST) can be used in sorting algorithms.
38
Practice Questions
● Merge Two Binary Trees
● Search in Binary Search Trees
● Same Tree
● Lowest Common Ancestor of Binary
Search Tree
● Validate Binary Search Trees
● Binary Tree Zigzag Level Order
Traversal
● Maximum Difference Between Node
and Ancestor
● Kth smallest Element in BST
● Maximum Sum BST in Binary Tree
39
Quote of the Day
40