CP-Binary TREEgraph
CP-Binary TREEgraph
Prepared By,
Dr. Nikita Bhatt
Ms. Vaishali Korea
19CE119 Hemit Rana
1
tree
2
Applications: Tree
• Routing table.
3
Tree data structure
4
Tree data structure
5
Tree data structure
Nonlinear data structure
hierarchical structure
6
Tree data structure
Nonlinear data structure
The non-linear data structure
cannot be implemented directly
7
Tree data structure
Nonlinear data structure
it is implemented using the linear
data structure like an array and
linked list.
8
Tree data structure
Array representation
2 1 4 11 12 NULL NULL
9
Tree data structure
LL representation
struct node{
int data;
struct node* left;
struct node* right;
};
10
Tree data structure
LL representation
11
Tree data structure
LL representation
12
Tree data structure
‘N’ary tree structure
struct Node
{
int data;
struct Node *first_child;
struct Node *second_child;
struct Node *third_child;
.
.
.
struct Node *nth_child;
};
13
Tree data structure
Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
14
Tree data structure
Preference of LL over array:
However,
15
Tree data structure
Different types of trees
1. Binary tree
16
Tree data structure
Different types of trees
1. Binary tree
17
Tree data structure
Different types of trees
1. Binary tree
2. Binary search tree
3. AVL tree
18
Tree data structure
Different types of trees
1. Binary tree
2. Binary search tree
3. AVL tree
4. heap
19
Tree data structure
Different types of trees complete binary tree
20
Tree data structure
Different types of trees
1. Binary tree
2. Binary search tree
3. AVL tree
4. heap
21
Tree data structure
Different types of trees
1. Binary tree
2. Binary search tree
3. AVL tree
4. heap
22
Tree data structure
Different types of trees Min heap:
4. heap
23
Tree data structure
Different types of trees Max heap:
4. heap
24
Tree operations
insert delete search
Height balanced
O(log2N) O(log2N) O(log2N)
tree (AVL)
25
Problem 1 : Same Tree https://leetcode.com/problems/same-tree/description/
Problem description:
Given the roots of two binary trees p and q, write a function to check if they are the
same or not.
Two binary trees are considered the same if they are structurally identical, and the
nodes have the same value.
Example 1:
Input: p = [1,2,3], q = [1,2,3]
Output: true
26
Problem 1 : Same Tree (continue)
Problem description:
Given the roots of two binary trees p and q, write a function to check if they are the
same or not.
Two binary trees are considered the same if they are structurally identical, and the
nodes have the same value.
Example 2:
Input: p = [1,2], q = [1,null,2]
Output: false
27
Problem 1 : Same Tree (continue)
Problem description:
Given the roots of two binary trees p and q, write a function to check if they are the
same or not.
Two binary trees are considered the same if they are structurally identical, and the
nodes have the same value.
Example 3:
Input: p = [1,2,1], q = [1,1,2]
Output: false
28
Problem 1 : Same Tree (continue)
Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
29
Problem 1 : Same Tree (continue)
P Q
1 1
1 1 1 1
30
Problem 1 : Same Tree (continue)
P Q
1 1
1 1 1 1
31
Problem 1 : Same Tree (continue)
P Q
1 1
1 1 1 1
32
Problem 1 : Same Tree (continue)
P Q
1 1
1 1 1 1
True
33
Problem 1 : Same Tree (continue)
P Q
1 1
2 1 3 1
34
Problem 1 : Same Tree (continue)
P Q
1 1
2 1 3 1
35
Problem 1 : Same Tree (continue)
P Q
1 1
2 1 3 1
False
36
Problem 1 : Same Tree (continue)
P Q
1 1
2 1 1
37
Problem 1 : Same Tree (continue)
P Q
1 1
2 1 1
38
Problem 1 : Same Tree (continue)
P Q
1 1
2 1 ? 1
P Q
False
39
Problem 1 : Same Tree (continue)
40
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
41
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
42
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
43
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
44
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
45
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
46
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
47
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
48
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
49
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
50
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
51
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
52
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
53
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
54
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
55
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
56
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
p
if(!p && !q)//if both are null
return true; 1
if((!p && q) || (p && !q))//if one is null and other is not
return false;
2 3
if(p->val != q->val)//if value is different
return false; q
1
bool lval = isSameTree(p->left, q->left); true
bool rval = isSameTree(p->right, q->right);
2 3
return lval && rval;
}
57
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
58
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
59
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
60
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
61
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
62
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
63
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
64
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
65
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
66
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
67
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
68
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
p
if(!p && !q)//if both are null
return true; 1
if((!p && q) || (p && !q))//if one is null and other is not
return false;
2 3
if(p->val != q->val)//if value is different
return false; q
1
bool lval = isSameTree(p->left, q->left);
bool rval = isSameTree(p->right, q->right); true
2 3
return lval && rval;
}
69
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
p
if(!p && !q)//if both are null
return true; 1
if((!p && q) || (p && !q))//if one is null and other is not
return false;
2 3
if(p->val != q->val)//if value is different
return false; q
1
bool lval = isSameTree(p->left, q->left);
bool rval = isSameTree(p->right, q->right);
2 3
return lval && rval; True && True
}
70
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
71
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
72
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
p
if(!p && !q)//if both are null
return true; 1
if((!p && q) || (p && !q))//if one is null and other is not
return false;
2
73
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
p
if(!p && !q)//if both are null
return true; 1
if((!p && q) || (p && !q))//if one is null and other is not
return false;
2
74
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
p
if(!p && !q)//if both are null
return true; 1
if((!p && q) || (p && !q))//if one is null and other is not
return false;
2
75
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
p
if(!p && !q)//if both are null
return true; 1
if((!p && q) || (p && !q))//if one is null and other is not
return false;
2
76
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
p
if(!p && !q)//if both are null
return true; 1
if((!p && q) || (p && !q))//if one is null and other is not
return false;
2
77
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
p
if(!p && !q)//if both are null
return true; 1
if((!p && q) || (p && !q))//if one is null and other is not
return false;
2
78
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
79
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
80
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
81
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
82
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
83
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {
84
Problem 2: Binary Tree Right View
https://leetcode.com/problems/binary-tree-right-side-view/description/
Problem description:
Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of
the nodes you can see ordered from top to bottom.
Example 1:
Input: root = [1,2,3,null,5,null,4]
Output: [1,3,4]
Example 2:
Input: root = [1,null,3]
Output: [1,3]
Example 3:
Input: root = []
Output: []
85
Problem 2: Binary Tree Right View (continue)
2 3
86
Problem 2: Binary Tree Right View (continue)
2 3
87
Problem 2: Binary Tree Right View (continue)
2 3
4 5
88
Problem 2: Binary Tree Right View (continue)
2 3
4 5
89
Problem 2: Binary Tree Right View (continue)
2 3
4 5
90
Problem 2: Binary Tree Right View (continue)
ans = [1]
2 3
4 5
91
Problem 2: Binary Tree Right View (continue)
ans = [1,3]
2 3
4 5
92
Problem 2: Binary Tree Right View (continue)
ans = [1,3]
2 3
4 5
93
Problem 2: Binary Tree Right View (continue)
ans = [1,3]
2 3
4 5
94
Problem 2: Binary Tree Right View (continue)
ans = [1,3]
2 3
4 5
95
Problem 2: Binary Tree Right View (continue)
ans = [1,3]
1
Why???
2 3
4 5
96
Problem 2: Binary Tree Right View (continue)
1
Right view of this
level is already
stored
2 3
4 5
97
Problem 2: Binary Tree Right View (continue)
1
level visit tracking
required
2 3
4 5
98
Problem 2: Binary Tree Right View (continue)
2 3
4 5
99
Problem 2: Binary Tree Right View (continue)
ans = [1]
Level = 0
1
2 3
4 5
100
Problem 2: Binary Tree Right View (continue)
ans = [1,3]
Level = 1
1
2 3
4 5
101
Problem 2: Binary Tree Right View (continue)
ans = [1,3]
Level = 2
1
2 3
4 5
102
Problem 2: Binary Tree Right View (continue)
ans = [1,3]
Level = 2
1
2 3
4 5
103
Problem 2: Binary Tree Right View (continue)
ans = [1,3]
Level = 0
1
2 3
4 5
104
Problem 2: Binary Tree Right View (continue)
ans = [1,3]
Level = 1
1
2 3
4 5
105
Problem 2: Binary Tree Right View (continue)
Notice the size of
ans = [1,3]
Level = 1
ans/view is the
1 maximum level
visited for right view
2 3
4 5
106
Problem 2: Binary Tree Right View (continue)
Check if
ans = [1,3]
Level = 1
level == ans.size()
1
2 3
4 5
107
Problem 2: Binary Tree Right View (continue)
Check if
ans = [1,3]
Level = 1
level == ans.size()
1
1 != 2
2 3
4 5
108
Problem 2: Binary Tree Right View (continue)
Check if
ans = [1,3]
Level = 2
level == ans.size()
1
2 3
4 5
109
Problem 2: Binary Tree Right View (continue)
Check if
ans = [1,3]
Level = 2
level == ans.size()
1
2 == 2
2 3
4 5
110
Problem 2: Binary Tree Right View (continue)
ans = [1,3,5]
Level = 2
1
2 3
4 5
111
Problem 2: Binary Tree Right View (continue)
ans = [1,3,5]
Level = 3
1
2 3
4 5
112
Problem 2: Binary Tree Right View (continue)
ans = [1,3,5]
Level = 3
1
2 3
4 5
113
Problem 2: Binary Tree Right View (continue)
ans = [1,3,5]
Level = 1
1
2 3
4 5
114
Problem 2: Binary Tree Right View (continue)
ans = [1,3,5]
Level = 1
1
2 3
4 5
115
Problem 2: Binary Tree Right View (continue)
ans = [1,3,5]
Level = 2
1
2 3
4 5
116
Problem 2: Binary Tree Right View (continue)
Check if
ans = [1,3,5]
Level = 2
level == ans.size()
1
2 3
4 5
117
Problem 2: Binary Tree Right View (continue)
Check if
ans = [1,3,5]
Level = 2
level == ans.size()
1 2 != 3
2 3
4 5
118
Problem 2: Binary Tree Right View (continue)
Check if
ans = [1,3,5]
Level = 2
level == ans.size()
1 2 != 3
2 3
4 5
119
Problem 2: Binary Tree Right View (continue)
ans = [1,3,5]
Level = 3
1
2 3
4 5
120
Problem 2: Binary Tree Right View (continue)
ans = [1,3,5]
Level = 3
1
2 3
4 5
121
Problem 2: Binary Tree Right View (continue)
ans = [1,3,5]
Level = 2
1
2 3
4 5
122
Problem 2: Binary Tree Right View (continue)
ans = [1,3,5]
Level = 1
1
2 3
4 5
123
Problem 2: Binary Tree Right View (continue)
ans = [1,3,5]
Level = 0
1
2 3
4 5
124
Problem 2: Binary Tree Right View (continue)
ans = [1,3,5]
1
2 3
4 5
125
Problem 2: Binary Tree Right View (continue)
ans = [1,3,5]
1
2 3
4 5
if(ans.size()==level)
ans.push_back(root->val);
recursion(root->right,level+1,ans);
recursion(root->left,level+1,ans);
}
127
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
2 2
ans.push_back(root->val);
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans);
}
128
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
2 2
ans.push_back(root->val);
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans);
}
129
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
2 2
ans.push_back(root->val);
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans);
}
130
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
2 2
ans.push_back(root->val);
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans);
}
131
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
2 2
ans.push_back(root->val);
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans);
}
132
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
2 2
ans.push_back(root->val);
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans);
}
133
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
2 2
ans.push_back(root->val);
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans);
}
134
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
2 2
ans.push_back(root->val);
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans);
}
135
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
2 2
ans.push_back(root->val);
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans); NULL
}
136
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
return 2 2
ans.push_back(root->val);
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans); NULL
}
137
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
return 2 2
ans.push_back(root->val);
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans);
}
138
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
2 2
ans.push_back(root->val);
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans); NULL
}
139
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
return 2 2
ans.push_back(root->val);
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans); NULL
}
140
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
return 2 2
ans.push_back(root->val);
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans);
}
141
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
2 2
ans.push_back(root->val);
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans);
}
142
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
2 2
ans.push_back(root->val);
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans);
}
143
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
2 2
ans.push_back(root->val);
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans);
}
144
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
2 2
ans.push_back(root->val); 2 != 1
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans);
}
145
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
2 2
ans.push_back(root->val);
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans);
}
146
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
2 2
ans.push_back(root->val);
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans);
}
147
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
2 2
ans.push_back(root->val); 2 == 2
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans);
}
148
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
2 2
ans.push_back(root->val); 2 == 2
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans);
}
149
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
2 2
ans.push_back(root->val);
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans);
}
150
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
2 2
ans.push_back(root->val);
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans);
} NULL
vector<int> rightSideView(TreeNode* root) { level 3
vector<int> ans;
recursion(root,0,ans);
return ans;
}
ans : [1,2,5 ]
151
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
2 2
ans.push_back(root->val);
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans);
} NULL
vector<int> rightSideView(TreeNode* root) { level 3
vector<int> ans;
recursion(root,0,ans);
return ans;
}
ans : [1,2,5 ]
152
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
2 2
ans.push_back(root->val);
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans);
}
153
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
2 2
ans.push_back(root->val);
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans);
} NULL
vector<int> rightSideView(TreeNode* root) { level 3
vector<int> ans;
recursion(root,0,ans);
return ans;
}
ans : [1,2,5 ]
154
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
2 2
ans.push_back(root->val);
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans);
} NULL
vector<int> rightSideView(TreeNode* root) { level 3
vector<int> ans;
recursion(root,0,ans);
return ans;
}
ans : [1,2,5 ]
155
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
2 2
ans.push_back(root->val);
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans);
}
156
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
2 2
ans.push_back(root->val);
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans);
}
157
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
2 2
ans.push_back(root->val); 3 != 2
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans);
}
158
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
2 2
ans.push_back(root->val);
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans);
} NULL
vector<int> rightSideView(TreeNode* root) { level 3
vector<int> ans;
recursion(root,0,ans);
return ans;
}
ans : [1,2,5 ]
159
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
2 2
ans.push_back(root->val);
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans);
} NULL
vector<int> rightSideView(TreeNode* root) { level 3
vector<int> ans;
recursion(root,0,ans);
return ans;
}
ans : [1,2,5 ]
160
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
2 2
ans.push_back(root->val);
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans);
} NULL
vector<int> rightSideView(TreeNode* root) { level 3
vector<int> ans;
recursion(root,0,ans);
return ans;
}
ans : [1,2,5 ]
161
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
2 2
ans.push_back(root->val);
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans);
} NULL
vector<int> rightSideView(TreeNode* root) { level 3
vector<int> ans;
recursion(root,0,ans);
return ans;
}
ans : [1,2,5 ]
162
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
2 2
ans.push_back(root->val);
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans);
}
163
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
2 2
ans.push_back(root->val);
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans);
}
164
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
2 2
ans.push_back(root->val);
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans);
}
165
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
2 2
ans.push_back(root->val);
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans);
}
166
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
2 2
ans.push_back(root->val);
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans);
}
167
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){ 1
if(root==NULL)
return;
if(ans.size()==level)
2 2
ans.push_back(root->val);
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans);
}
if(ans.size()==level)
2 2
ans.push_back(root->val);
recursion(root->right,level+1,ans); 4 5
recursion(root->left,level+1,ans);
}
task : 1
left view of binary tree
2 2
4 5
5
170
Extra problem: Maximum Level Sum of a Binary Tree
https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/description/
Problem description:
Given the root of a binary tree, the level of its root is 1, the level of its children is 2, and so on.
Return the smallest level x such that the sum of all the values of nodes at level x is maximal.
Example 1:
Input: root = [1,7,0,7,-8,null,null]
Output: 2
Explanation:
Level 1 sum = 1.
Level 2 sum = 7 + 0 = 7.
Level 3 sum = 7 + -8 = -1.
So we return the level with the maximum sum which is level 2.
Example 2:
Input: root = [989,null,10250,98693,-89388,null,null,null,-32127]
Output: 2
171
Problem 4: Maximum Level Sum of a Binary Tree
Solution :
while(!q.empty()){
int sum = 0;
int size = q.size();
int maxLevelSum(TreeNode* root) for(int i = 0; i < size; i++){
{ TreeNode* curr = q.front();
Example:
root : 20
left- boundary nodes: 8
leaf nodes: 4 10 14 25
right – boundary nodes: 22
173
Problem 5: boundary traversal of a binary tree
https://www.geeksforgeeks.org/boundary-traversal-of-binary-tree/
Print boundary function
void printBoundary(Node* root)
{
if (root == nullptr)
return;
if (root->left) {
printLeaves(root->left);
printLeaves(root->right);
}
176
Problem 5: boundary traversal of a binary tree
https://www.geeksforgeeks.org/boundary-traversal-of-binary-tree/
right boundary in bottom-up manner.
void printBoundaryRight(Node* root)
{
if (root == nullptr)
return;
if (root->right) {
// to ensure bottom up order, first call for right
// subtree, then print this node
printBoundaryRight(root->right);
cout << root->data << " ";
}
else if (root->left) {
printBoundaryRight(root->left);
cout << root->data << " ";
}
// do nothing if it is a leaf node, this way we avoid
// duplicates in output
}
177
GRAPH
178
Applications: Graph
• Social Networks
• Transportation Networks
• Recommendation Systems
179
Graph data structure
180
Graph data structure
181
Graph data structure
Nonlinear data structure
182
Graph data structure
Adjacency Matrix
183
Graph data structure
Adjacency Matrix
184
Graph data structure
Adjacency List
185
Graph data structure
Adjacency List
186
Graph data structure
Different types of graphs
Undirected Graph
187
Graph data structure
Different types of graphs
Directed Graph
188
Graph data structure
Different types of graphs
Weighted Graph
189
Graph data structure
Different types of graphs
Finite Graph
190
Graph data structure
Different types of graphs
Infinite Graph
191
Graph data structure
Different types of graphs
Trivial Graph
192
Graph data structure
Different types of graphs
Simple Graph
193
Graph data structure
Different types of graphs
Multi Graph
194
Graph data structure
Different types of graphs
Null Graph
195
Graph data structure
Different types of graphs
Complete Graph
196
Graph data structure
Different types of graphs
Pseudo Graph
197
Graph data structure
Different types of graphs
Regular Graph
198
Graph data structure
Different types of graphs
199
Graph data structure
Graph terminologies
adjacent.
degree
200
Graph data structure
Graph terminologies
out-degree
in-degree
201
Problem 1 : Town Judgehttps://leetcode.com/problems/find-the-town-judge/description/
Problem description:
In a town, there are n people labeled from 1 to n. There is a rumor that one of these
people is secretly the town judge.
Return the label of the town judge if the town judge exists and can be identified, or
return -1 otherwise.
202
Problem 1 : Town Judge (continue)
Example 1:
Input: n = 2, trust = [[1,2]]
Output: 2
Example 2:
Input: n = 3, trust = [[1,3],[2,3]]
Output: 3
Example 3:
Input: n = 3, trust = [[1,3],[2,3],[3,1]]
Output: -1
203
Problem 1 : Town Judge (continue)
Constraints:
204
Problem 1 : Town Judge (continue)
In a town, there are n people labeled from 1 to n.
1 2 3 N
205
Problem 1 : Town Judge (continue)
There is a rumor that one of these people is secretly the town judge.
1 2 3 X N
judge
206
Problem 1 : Town Judge (continue)
If the town judge exists, then:
1 2 3 N
X judge
207
Problem 1 : Town Judge (continue)
If the town judge exists, then:
2. Everybody (except for the town judge) trusts the town judge.
1 2 3 N
X judge
208
Problem 1 : Town Judge (continue)
If the town judge exists, then:
1 2 3 N
X judge
209
Problem 1 : Town Judge (continue)
You are given an array trust where trust[i] = [ai, bi] representing that the
person labeled ai trusts the person labeled bi. If a trust relationship does
not exist in trust array, then such a trust relationship does not exist.
N=4
Trust = {1,2}
{1,4}
{2,3}
{2,4}
{3,1}
{3,4}
210
Problem 1 : Town Judge (continue)
Return the label of the town judge if the town judge exists and can be
identified, or return -1 otherwise.
N=4
Trust = {1,2}
{1,4}
{2,3}
{2,4}
{3,1}
{3,4}
211
Problem 1 : Town Judge (continue)
N=4
2 3
Trust =
{1,2}
{1,4}
{2,3}
{2,4}
1 4 {3,1}
{3,4}
Judge = 4
212
Problem 1 : Town Judge (continue)
N=5
2 3
Trust =
{1,2}
{1,4}
{2,3}
{2,4}
1 4 {3,1}
{3,4}
5
Judge = 4
213
Problem 1 : Town Judge (continue)
N=5
2 3
Trust =
{1,2}
{1,4}
{2,3}
{2,4}
1 4 {3,1}
{3,4}
{4,5}
5 {3,5}
Judge = -1
214
Problem 1 : Town Judge (continue)
int findJudge(int n, vector<vector<int>>& trust) {
N=4
if(n==1) return 1;
Trust =
unordered_map<int, int> indegree, outdegree; {1,2}
{1,4}
(Continue…)
{2,3}
{2,4}
{3,1}
{3,4}
indegree
outdegree
215
Problem 1 : Town Judge (continue)
for(auto i: trust){ Trust
int trusts = i[0];
int trusted = i[1]; 1,2 1,4 2,3 2,4 3,1 3,4
indegree[trusted]++;
outdegree[trusts]++;
}
for(auto i: indegree){
int node = i.first;
int in = i.second;
if(in == n-1 && outdegree[node]==0) first second
return node; indegree
}
216
Problem 1 : Town Judge (continue)
for(auto i: trust){ Trust
int trusts = i[0];
int trusted = i[1]; 1,2 1,4 2,3 2,4 3,1 3,4
indegree[trusted]++; i
outdegree[trusts]++;
}
for(auto i: indegree){
int node = i.first;
int in = i.second;
if(in == n-1 && outdegree[node]==0) first second
return node; indegree
}
217
Problem 1 : Town Judge (continue)
for(auto i: trust){ Trust
int trusts = i[0];
int trusted = i[1]; 1,2 1,4 2,3 2,4 3,1 3,4
indegree[trusted]++; i
outdegree[trusts]++;
} Trusts = 1
for(auto i: indegree){
Trusted = 2
int node = i.first;
int in = i.second;
if(in == n-1 && outdegree[node]==0) first second
return node; indegree
}
218
Problem 1 : Town Judge (continue)
for(auto i: trust){ Trust
int trusts = i[0];
int trusted = i[1]; 1,2 1,4 2,3 2,4 3,1 3,4
indegree[trusted]++; i
outdegree[trusts]++;
} Trusts = 1
for(auto i: indegree){
Trusted = 2
int node = i.first;
indegree first second
int in = i.second;
if(in == n-1 && outdegree[node]==0) 2 1
return node;
}
first second
return -1; outdegree
} 1 1
219
Problem 1 : Town Judge (continue)
for(auto i: trust){ Trust
int trusts = i[0];
int trusted = i[1]; 1,2 1,4 2,3 2,4 3,1 3,4
indegree[trusted]++; i
outdegree[trusts]++;
} Trusts = 1
for(auto i: indegree){
Trusted = 4
int node = i.first;
indegree first second
int in = i.second;
if(in == n-1 && outdegree[node]==0) 2 1
return node;
}
first second
return -1; outdegree
} 1 1
220
Problem 1 : Town Judge (continue)
for(auto i: trust){ Trust
int trusts = i[0];
int trusted = i[1]; 1,2 1,4 2,3 2,4 3,1 3,4
indegree[trusted]++; i
outdegree[trusts]++;
} Trusts = 1
for(auto i: indegree){
Trusted = 4
int node = i.first;
indegree first second
int in = i.second;
if(in == n-1 && outdegree[node]==0) 2 1
return node; 4 1
}
first second
return -1; outdegree
} 1 2
221
Problem 1 : Town Judge (continue)
for(auto i: trust){ Trust
int trusts = i[0];
int trusted = i[1]; 1,2 1,4 2,3 2,4 3,1 3,4
indegree[trusted]++; i
outdegree[trusts]++;
} Trusts = 2
for(auto i: indegree){
Trusted = 3
int node = i.first;
indegree first second
int in = i.second;
if(in == n-1 && outdegree[node]==0) 2 1
return node; 4 1
}
first second
return -1; outdegree
} 1 2
222
Problem 1 : Town Judge (continue)
for(auto i: trust){
int trusts = i[0]; Trust 1,2 1,4 2,3 2,4 3,1 3,4
int trusted = i[1]; i
indegree[trusted]++; Trusts = 2
outdegree[trusts]++; Trusted = 3
}
indegree outdegree
for(auto i: indegree){ first second first second
int node = i.first;
2 1 1 2
int in = i.second;
if(in == n-1 && outdegree[node]==0) 4 1 2 1
return node; 3 1
}
return -1;
}
223
Problem 1 : Town Judge (continue)
for(auto i: trust){
int trusts = i[0]; Trust 1,2 1,4 2,3 2,4 3,1 3,4
int trusted = i[1]; i
indegree[trusted]++; Trusts = 2
outdegree[trusts]++; Trusted = 4
}
indegree outdegree
for(auto i: indegree){ first second first second
int node = i.first;
2 1 1 2
int in = i.second;
if(in == n-1 && outdegree[node]==0) 4 1 2 1
return node; 3 1
}
return -1;
}
224
Problem 1 : Town Judge (continue)
for(auto i: trust){
int trusts = i[0]; Trust 1,2 1,4 2,3 2,4 3,1 3,4
int trusted = i[1]; i
indegree[trusted]++; Trusts = 2
outdegree[trusts]++; Trusted = 4
}
indegree outdegree
for(auto i: indegree){ first second first second
int node = i.first;
2 1 1 2
int in = i.second;
if(in == n-1 && outdegree[node]==0) 4 2 2 2
return node; 3 1
}
return -1;
}
225
Problem 1 : Town Judge (continue)
for(auto i: trust){
int trusts = i[0]; Trust 1,2 1,4 2,3 2,4 3,1 3,4
int trusted = i[1]; i
indegree[trusted]++; Trusts = 3
outdegree[trusts]++; Trusted = 1
}
indegree outdegree
for(auto i: indegree){ first second first second
int node = i.first;
2 1 1 2
int in = i.second;
if(in == n-1 && outdegree[node]==0) 4 2 2 2
return node; 3 1
}
return -1;
}
226
Problem 1 : Town Judge (continue)
for(auto i: trust){
int trusts = i[0]; Trust 1,2 1,4 2,3 2,4 3,1 3,4
int trusted = i[1]; i
indegree[trusted]++; Trusts = 3
outdegree[trusts]++; Trusted = 1
}
indegree outdegree
for(auto i: indegree){ first second first second
int node = i.first;
2 1 1 2
int in = i.second;
if(in == n-1 && outdegree[node]==0) 4 2 2 2
return node; 3 1 3 1
}
1 1
return -1;
}
227
Problem 1 : Town Judge (continue)
for(auto i: trust){
int trusts = i[0]; Trust 1,2 1,4 2,3 2,4 3,1 3,4
int trusted = i[1]; i
indegree[trusted]++; Trusts = 3
outdegree[trusts]++; Trusted = 1
}
indegree outdegree
for(auto i: indegree){ first second first second
int node = i.first;
2 1 1 2
int in = i.second;
if(in == n-1 && outdegree[node]==0) 4 2 2 2
return node; 3 1 3 1
}
1 1
return -1;
}
228
Problem 1 : Town Judge (continue)
for(auto i: trust){
int trusts = i[0]; Trust 1,2 1,4 2,3 2,4 3,1 3,4
int trusted = i[1]; i
indegree[trusted]++; Trusts = 3
outdegree[trusts]++; Trusted = 4
}
indegree outdegree
for(auto i: indegree){ first second first second
int node = i.first;
2 1 1 2
int in = i.second;
if(in == n-1 && outdegree[node]==0) 4 3 2 2
return node; 3 1 3 2
}
1 1
return -1;
}
229
Problem 1 : Town Judge (continue)
for(auto i: trust){
int trusts = i[0]; Trust 1,2 1,4 2,3 2,4 3,1 3,4
int trusted = i[1];
indegree[trusted]++; Trusts = 3
outdegree[trusts]++; Trusted = 4
}
indegree outdegree
for(auto i: indegree){ first second first second
int node = i.first;
int in = i.second; i 1 1 1 2
if(in == n-1 && outdegree[node]==0) 2 1 2 2
return node; 3 1 3 2
}
4 3
return -1;
}
230
Problem 1 : Town Judge (continue)
for(auto i: trust){
int trusts = i[0]; Trust 1,2 1,4 2,3 2,4 3,1 3,4
int trusted = i[1];
indegree[trusted]++;
outdegree[trusts]++;
} node = 1
in = 1 indegree outdegree
for(auto i: indegree){ first second first second
int node = i.first;
int in = i.second; i 1 1 1 2
if(in == n-1 && outdegree[node]==0) 2 1 2 2
return node; 3 1 3 2
}
4 3
return -1;
}
231
Problem 1 : Town Judge (continue)
for(auto i: trust){
int trusts = i[0]; Trust 1,2 1,4 2,3 2,4 3,1 3,4
int trusted = i[1];
indegree[trusted]++;
outdegree[trusts]++; node = 1
} in = 1 != 3 indegree outdegree
for(auto i: indegree){ first second first second
int node = i.first;
int in = i.second; i 1 1 1 2
if(in == n-1 && outdegree[node]==0) 2 1 2 2
return node; 3 1 3 2
}
4 3
return -1;
}
232
Problem 1 : Town Judge (continue)
for(auto i: trust){
int trusts = i[0]; Trust 1,2 1,4 2,3 2,4 3,1 3,4
int trusted = i[1];
indegree[trusted]++;
outdegree[trusts]++; node = 2
} in = 1 != 3 indegree outdegree
for(auto i: indegree){ first second first second
int node = i.first;
1 1 1 2
int in = i.second;
if(in == n-1 && outdegree[node]==0) i 2 1 2 2
return node; 3 1 3 2
}
4 3
return -1;
}
233
Problem 1 : Town Judge (continue)
for(auto i: trust){
int trusts = i[0]; Trust 1,2 1,4 2,3 2,4 3,1 3,4
int trusted = i[1];
indegree[trusted]++;
outdegree[trusts]++; node = 3
} in = 1 != 3 indegree outdegree
for(auto i: indegree){ first second first second
int node = i.first;
1 1 1 2
int in = i.second;
if(in == n-1 && outdegree[node]==0) 2 1 2 2
return node;
} i 3 1 3 2
4 3
return -1;
}
234
Problem 1 : Town Judge (continue)
for(auto i: trust){
int trusts = i[0]; Trust 1,2 1,4 2,3 2,4 3,1 3,4
int trusted = i[1];
indegree[trusted]++;
outdegree[trusts]++; node = 3
} in = 1 != 3 indegree outdegree
for(auto i: indegree){ first second first second
int node = i.first;
1 1 1 2
int in = i.second;
if(in == n-1 && outdegree[node]==0) 2 1 2 2
return node;
} i 3 1 3 2
4 3
return -1;
}
235
Problem 1 : Town Judge (continue)
for(auto i: trust){
int trusts = i[0]; Trust 1,2 1,4 2,3 2,4 3,1 3,4
int trusted = i[1];
indegree[trusted]++;
outdegree[trusts]++; node = 4
} in = 3 == 3 indegree outdegree
for(auto i: indegree){ first second first second
int node = i.first;
1 1 1 2
int in = i.second;
if(in == n-1 && outdegree[node]==0) 2 1 2 2
return node; 3 1 3 2
}
i 4 3
return -1;
} outdegree[4]=0
236
Problem 1 : Town Judge (continue)
for(auto i: trust){
int trusts = i[0]; Trust 1,2 1,4 2,3 2,4 3,1 3,4
int trusted = i[1];
indegree[trusted]++;
outdegree[trusts]++; node = 4
} in = 3 == 3 indegree outdegree
for(auto i: indegree){ first second first second
int node = i.first;
1 1 1 2
int in = i.second;
if(in == n-1 && outdegree[node]==0) 2 1 2 2
return node; 3 1 3 2
}
return 4 i 4 3
return -1;
} outdegree[4]=0
237
Problem 1 : Town Judge (continue)
for(auto i: trust){
int trusts = i[0]; Trust 1,2 1,4 2,3 2,4 3,1 3,4
int trusted = i[1];
238
Problem 1 : Town Judge (continue)
for(auto i: trust){
int trusts = i[0]; Trust 1,2 1,4 2,3 2,4 3,1 3,4
int trusted = i[1];
239
Problem 1 : Town Judge (continue)
for(auto i: trust){
int trusts = i[0]; Trust 1,2 1,4 2,3 2,4 3,1 3,4 4,5 3,5
int trusted = i[1];
indegree[trusted]++;
N =5
outdegree[trusts]++;
}
indegree outdegree
for(auto i: indegree){ first second first second
int node = i.first;
1 1 1 2
int in = i.second;
if(in == n-1 && outdegree[node]==0) 2 1 2 2
return node; 3 1 3 3
}
4 3 4 1
return -1; 5 2
}
240
Problem 1 : Town Judge (continue)
for(auto i: trust){
int trusts = i[0];
int trusted = i[1];
indegree[trusted]++;
outdegree[trusts]++;
}
for(auto i: indegree){
int node = i.first;
int in = i.second;
if(in == n-1 && outdegree[node]==0)
return node;
}
Time complexity: O(N)
return -1;
} Space complexity: O(N)
};
241
Problem 2 : Course Schedule
https://leetcode.com/problems/course-schedule/description/
Problem description:
There are a total of numCourses courses you have to take, labeled from 0 to
numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi]
indicates that you must take course bi first if you want to take course ai.
For example, the pair [0, 1], indicates that to take course 0 you have to first take
course 1.
Return true if you can finish all courses. Otherwise, return false.
Example 1:
Input: numCourses = 2, prerequisites = [[1,0]]
Output: true
Explanation: There are a total of 2 courses to take.
To take course 1 you should have finished course 0. So it is possible.
242
Problem 2 : Course Schedule
https://leetcode.com/problems/course-schedule/description/
Problem description:
There are a total of numCourses courses you have to take, labeled from 0 to
numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi]
indicates that you must take course bi first if you want to take course ai.
For example, the pair [0, 1], indicates that to take course 0 you have to first take
course 1.
Return true if you can finish all courses. Otherwise, return false.
Example 2:
Input: numCourses = 2, prerequisites = [[1,0],[0,1]]
Output: false
Explanation: There are a total of 2 courses to take.
To take course 1 you should have finished course 0, and to take course 0 you should
also have finished course 1. So it is impossible.
243
Problem 2 : Course Schedule (continue)
There are a total of numCourses courses you have to take, labeled from 0 to
numCourses - 1.
0 1 2 N-1
244
Problem 2 : Course Schedule (continue)
You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that
you must take course bi first if you want to take course ai.
numCourses = 4
prerequisites =
{0,1}
{0,3}
{1,2}
{1,3}
245
Problem 2 : Course Schedule (continue)
You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that
you must take course bi first if you want to take course ai.
numCourses = 4
0 2
prerequisites =
{0,1}
{0,3}
{1,2}
1 3 {1,3}
246
Problem 2 : Course Schedule (continue)
You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that
you must take course bi first if you want to take course ai.
numCourses = 4
0 2
prerequisites =
{0,1}
{0,3}
{1,2}
1 3 {1,3}
TRUE
247
Problem 2 : Course Schedule (continue)
You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that
you must take course bi first if you want to take course ai.
numCourses = 4
0 2
prerequisites =
{0,1}
{0,3}
{1,2}
1 3 {1,3}
{2,0}
248
Problem 2 : Course Schedule (continue)
You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that
you must take course bi first if you want to take course ai.
numCourses = 4
0 2
prerequisites =
{0,1}
{0,3}
{1,2}
1 3 {1,3}
{2,0}
FALSE
249
Problem 2 : Course Schedule (continue)
1. find cycle
2. if present -> can not finish
3. otherwise -> can finish
250
Problem 2 : Course Schedule (continue)
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
if (numCourses == 1 || prerequisites.size()==0) return true;
vector<int> adj[numCourses];
for(int i=0;i<prerequisites.size();i++){
adj[prerequisites[i][0]].push_back( prerequisites[i][1]);
}
int cycleFlag=0;
vector<int> vis(numCourses,0);
for(int i=0;i<numCourses;i++){
if(!vis[i]){
if(dfs(i,-1,vis,adj)) cycleFlag = 1;
}
}
251
Problem 2 : Course Schedule (continue)
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
numCourses = 4
if (numCourses == 1 || prerequisites.size()==0) return true;
1 3
for(int i=0;i<numCourses;i++){
if(!vis[i]){
if(dfs(i,-1,vis,adj)) cycleFlag = 1;
}
}
252
Problem 2 : Course Schedule (continue)
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
numCourses = 4
if (numCourses == 1 || prerequisites.size()==0) return true;
1 3
for(int i=0;i<numCourses;i++){
if(!vis[i]){
if(dfs(i,-1,vis,adj)) cycleFlag = 1;
}
}
253
Problem 2 : Course Schedule (continue)
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
numCourses = 4
if (numCourses == 1 || prerequisites.size()==0) return true;
1 3
for(int i=0;i<numCourses;i++){
if(!vis[i]){
if(dfs(i,-1,vis,adj)) cycleFlag = 1;
}
}
0 1 2 3
if(cycleFlag==0) return true; adj
return false; 0 0 0
}
254
Problem 2 : Course Schedule (continue)
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
prerequisites
if (numCourses == 1 || prerequisites.size()==0) return true;
0,1 0,3 1,2 1,3
vector<int> adj[numCourses];
for(int i=0;i<prerequisites.size();i++){
}
adj[prerequisites[i][0]].push_back( prerequisites[i][1]);
0 2
int cycleFlag=0;
vector<int> vis(numCourses,0);
1 3
for(int i=0;i<numCourses;i++){
if(!vis[i]){
if(dfs(i,-1,vis,adj)) cycleFlag = 1;
}
}
0 1 2 3
if(cycleFlag==0) return true; adj
return false; 1
}
255
Problem 2 : Course Schedule (continue)
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
prerequisites
if (numCourses == 1 || prerequisites.size()==0) return true;
0,10,10,3 1,3
1,2 2,1
1,3
vector<int> adj[numCourses];
for(int i=0;i<prerequisites.size();i++){
}
adj[prerequisites[i][0]].push_back( prerequisites[i][1]);
0 2
int cycleFlag=0;
vector<int> vis(numCourses,0);
1 3
for(int i=0;i<numCourses;i++){
if(!vis[i]){
if(dfs(i,-1,vis,adj)) cycleFlag = 1;
}
} 0 1 2 3
if(cycleFlag==0) return true; adj 1
return false;
} 3
256
Problem 2 : Course Schedule (continue)
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
prerequisites
if (numCourses == 1 || prerequisites.size()==0) return true;
0,10,10,3 1,3
1,2 2,1
1,3
vector<int> adj[numCourses];
for(int i=0;i<prerequisites.size();i++){
}
adj[prerequisites[i][0]].push_back( prerequisites[i][1]);
0 2
int cycleFlag=0;
vector<int> vis(numCourses,0);
1 3
for(int i=0;i<numCourses;i++){
if(!vis[i]){
if(dfs(i,-1,vis,adj)) cycleFlag = 1;
}
} 0 1 2 3
if(cycleFlag==0) return true; adj 1 2
return false;
} 3
257
Problem 2 : Course Schedule (continue)
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
prerequisites
if (numCourses == 1 || prerequisites.size()==0) return true;
0,10,10,3 1,3
1,2 2,1
1,3
vector<int> adj[numCourses];
for(int i=0;i<prerequisites.size();i++){
}
adj[prerequisites[i][0]].push_back( prerequisites[i][1]);
0 2
int cycleFlag=0;
vector<int> vis(numCourses,0);
1 3
for(int i=0;i<numCourses;i++){
if(!vis[i]){
if(dfs(i,-1,vis,adj)) cycleFlag = 1;
}
} 0 1 2 3
if(cycleFlag==0) return true; adj 1 2
return false;
} 3 3
258
Problem 2 : Course Schedule (continue)
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
prerequisites
if (numCourses == 1 || prerequisites.size()==0) return true;
0,10,10,3 1,3
1,2 2,1
1,3
vector<int> adj[numCourses];
for(int i=0;i<prerequisites.size();i++){
}
adj[prerequisites[i][0]].push_back( prerequisites[i][1]);
0 2
int cycleFlag=0;
vector<int> vis(numCourses,0);
1 3
for(int i=0;i<numCourses;i++){
if(!vis[i]){
if(dfs(i,-1,vis,adj)) cycleFlag = 1;
}
} 0 1 2 3
if(cycleFlag==0) return true; adj 1 2
return false;
} 3 3
259
Problem 2 : Course Schedule (continue)
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
prerequisites
if (numCourses == 1 || prerequisites.size()==0) return true;
0,10,10,3 1,3
1,2 2,1
1,3
vector<int> adj[numCourses];
for(int i=0;i<prerequisites.size();i++){
}
adj[prerequisites[i][0]].push_back( prerequisites[i][1]);
0 2
int cycleFlag=0;
vector<int> vis(numCourses,0);
1 3
for(int i=0;i<numCourses;i++){
if(!vis[i]){
if(dfs(i,-1,vis,adj)) cycleFlag = 1;
}
} 0 1 2 3
if(cycleFlag==0) return true; adj 1 2
return false;
} 3 3
260
Problem 2 : Course Schedule (continue)
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
prerequisites
if (numCourses == 1 || prerequisites.size()==0) return true;
0,10,10,3 1,3
1,2 2,1
1,3
vector<int> adj[numCourses];
for(int i=0;i<prerequisites.size();i++){
}
adj[prerequisites[i][0]].push_back( prerequisites[i][1]);
0 2
int cycleFlag=0;
vector<int> vis(numCourses,0);
1 3
for(int i=0;i<numCourses;i++){
if(!vis[i]){
if(dfs(i,-1,vis,adj)) cycleFlag = 1;
}
} 0 1 2 3
if(cycleFlag==0) return true; adj 1 2
return false;
} 3 3
261
Problem 2 : Course Schedule (continue)
bool dfs(int node, int parent,vector<int> &vis, vector<int> adj[]) {
if(vis[node] == 0){
vis[node] = 1;
for(auto adjacentNode: adj[node]) {
0 2
// unvisited adjacent node
if(dfs(adjacentNode, node, vis, adj))
}
return true;
1 3
}
vis[node] = 2;
return false;
}
262
Problem 2 : Course Schedule (continue)
bool dfs(int node, int parent,vector<int> &vis, vector<int> adj[]) {
if(vis[node] == 0){
-1
0 2
vis[node] = 1;
for(auto adjacentNode: adj[node]) {
// unvisited adjacent node 1 3
if(dfs(adjacentNode, node, vis, adj))
return true;
0 1 2 3
} vis 0 0 0 0
}
0 1 2 3
vis[node] = 2;
return false; adj 1 2
}
3 3
263
Problem 2 : Course Schedule (continue)
bool dfs(int node, int parent,vector<int> &vis, vector<int> adj[]) {
if(vis[node] == 0){
0 2
vis[node] = 1;
for(auto adjacentNode: adj[node]) {
// unvisited adjacent node 1 3
if(dfs(adjacentNode, node, vis, adj))
return true;
0 1 2 3
} vis 0 0 0 0
}
0 1 2 3
vis[node] = 2;
return false; adj 1 2
}
3 3
265
Problem 2 : Course Schedule (continue)
bool dfs(int node, int parent,vector<int> &vis, vector<int> adj[]) {
vis[node] = 1;
for(auto adjacentNode: adj[node]) { 1 3
// unvisited adjacent node
if(dfs(adjacentNode, node, vis, adj))
return true;
0 1 2 3
} vis 1 0 0 0
}
0 1 2 3
vis[node] = 2;
return false; adj 1 2
}
3 3
266
Problem 2 : Course Schedule (continue)
bool dfs(int node, int parent,vector<int> &vis, vector<int> adj[]) {
vis[node] = 1;
for(auto adjacentNode: adj[node]) { 1 3
// unvisited adjacent node
if(dfs(adjacentNode, node, vis, adj))
return true;
0 1 2 3
} vis 1 0 0 0
}
0 1 2 3
vis[node] = 2;
return false; adj 1 2
}
3 3
267
Problem 2 : Course Schedule (continue)
bool dfs(int node, int parent,vector<int> &vis, vector<int> adj[]) {
vis[node] = 1;
for(auto adjacentNode: adj[node]) { 1 3
// unvisited adjacent node
if(dfs(adjacentNode, node, vis, adj))
return true;
0 1 2 3
} vis 1 0 0 0
}
0 1 2 3
vis[node] = 2;
return false; adj 1 2
}
3 3
268
Problem 2 : Course Schedule (continue)
bool dfs(int node, int parent,vector<int> &vis, vector<int> adj[]) {
vis[node] = 1;
for(auto adjacentNode: adj[node]) {
0 1 3
// unvisited adjacent node
if(dfs(adjacentNode, node, vis, adj))
return true;
0 1 2 3
} vis 1 0 0 0
}
0 1 2 3
vis[node] = 2;
return false; adj 1 2
}
3 3
269
Problem 2 : Course Schedule (continue)
bool dfs(int node, int parent,vector<int> &vis, vector<int> adj[]) {
vis[node] = 1;
for(auto adjacentNode: adj[node]) {
0 1 3
// unvisited adjacent node
if(dfs(adjacentNode, node, vis, adj))
return true;
0 1 2 3
} vis 1 0 0 0
}
0 1 2 3
vis[node] = 2;
return false; adj 1 2
}
3 3
270
Problem 2 : Course Schedule (continue)
bool dfs(int node, int parent,vector<int> &vis, vector<int> adj[]) {
vis[node] = 1;
for(auto adjacentNode: adj[node]) {
0 1 3
// unvisited adjacent node
if(dfs(adjacentNode, node, vis, adj))
return true;
0 1 2 3
} vis 1 0 0 0
}
0 1 2 3
vis[node] = 2;
return false; adj 1 2
}
3 3
271
Problem 2 : Course Schedule (continue)
bool dfs(int node, int parent,vector<int> &vis, vector<int> adj[]) {
vis[node] = 1;
for(auto adjacentNode: adj[node]) {
0 1 3
// unvisited adjacent node
if(dfs(adjacentNode, node, vis, adj))
return true;
0 1 2 3
} vis 1 1 0 0
}
0 1 2 3
vis[node] = 2;
return false; adj 1 2
}
3 3
272
Problem 2 : Course Schedule (continue)
bool dfs(int node, int parent,vector<int> &vis, vector<int> adj[]) {
vis[node] = 1;
for(auto adjacentNode: adj[node]) {
0 1 3
// unvisited adjacent node
if(dfs(adjacentNode, node, vis, adj))
return true;
0 1 2 3
} vis 1 1 0 0
}
0 1 2 3
vis[node] = 2;
return false; adj 1 2
}
3 3
273
Problem 2 : Course Schedule (continue)
bool dfs(int node, int parent,vector<int> &vis, vector<int> adj[]) {
vis[node] = 1;
for(auto adjacentNode: adj[node]) {
0 1 3
// unvisited adjacent node
if(dfs(adjacentNode, node, vis, adj))
return true;
0 1 2 3
} vis 1 1 0 0
}
0 1 2 3
vis[node] = 2;
return false; adj 1 2
}
3 3
274
Problem 2 : Course Schedule (continue)
bool dfs(int node, int parent,vector<int> &vis, vector<int> adj[]) {
1
if(vis[node] == 1) return true; -1 0 2
if(vis[node] == 0){
vis[node] = 1;
for(auto adjacentNode: adj[node]) {
0 1 3
// unvisited adjacent node
if(dfs(adjacentNode, node, vis, adj))
return true;
0 1 2 3
} vis 1 1 0 0
}
0 1 2 3
vis[node] = 2;
return false; adj 1 2
}
3 3
275
Problem 2 : Course Schedule (continue)
bool dfs(int node, int parent,vector<int> &vis, vector<int> adj[]) {
1
if(vis[node] == 1) return true; -1 0 2
if(vis[node] == 0){
vis[node] = 1;
for(auto adjacentNode: adj[node]) {
0 1 3
// unvisited adjacent node
if(dfs(adjacentNode, node, vis, adj))
return true;
0 1 2 3
} vis 1 1 0 0
}
0 1 2 3
vis[node] = 2;
return false; adj 1 2
}
3 3
276
Problem 2 : Course Schedule (continue)
bool dfs(int node, int parent,vector<int> &vis, vector<int> adj[]) {
1
if(vis[node] == 1) return true; -1 0 2
if(vis[node] == 0){
vis[node] = 1;
for(auto adjacentNode: adj[node]) {
0 1 3
// unvisited adjacent node
if(dfs(adjacentNode, node, vis, adj))
return true;
0 1 2 3
} vis 1 1 1 0
}
0 1 2 3
vis[node] = 2;
return false; adj 1 2
}
3 3
277
Problem 2 : Course Schedule (continue)
bool dfs(int node, int parent,vector<int> &vis, vector<int> adj[]) {
1
if(vis[node] == 1) return true; -1 0 2
if(vis[node] == 0){
vis[node] = 1;
for(auto adjacentNode: adj[node]) {
0 1 3
// unvisited adjacent node
if(dfs(adjacentNode, node, vis, adj))
return true;
0 1 2 3
} vis 1 1 1 0
}
0 1 2 3
vis[node] = 2;
return false; adj 1 2
}
3 3
278
Problem 2 : Course Schedule (continue)
bool dfs(int node, int parent,vector<int> &vis, vector<int> adj[]) {
1
if(vis[node] == 1) return true; -1 0 2
if(vis[node] == 0){
vis[node] = 1;
for(auto adjacentNode: adj[node]) {
0 1 3
// unvisited adjacent node
if(dfs(adjacentNode, node, vis, adj))
return true;
0 1 2 3
} vis 1 1 2 0
}
0 1 2 3
vis[node] = 2;
return false; adj 1 2
}
3 3
279
Problem 2 : Course Schedule (continue)
bool dfs(int node, int parent,vector<int> &vis, vector<int> adj[]) {
1
if(vis[node] == 1) return true; -1 0 2
if(vis[node] == 0){
vis[node] = 1;
for(auto adjacentNode: adj[node]) {
0 1 3
// unvisited adjacent node
if(dfs(adjacentNode, node, vis, adj))
return true;
0 1 2 3
} vis 1 1 2 0
}
0 1 2 3
vis[node] = 2;
return false; adj 1 2
}
3 3
280
Problem 2 : Course Schedule (continue)
bool dfs(int node, int parent,vector<int> &vis, vector<int> adj[]) {
1
if(vis[node] == 1) return true; -1 0 2
if(vis[node] == 0){
vis[node] = 1;
for(auto adjacentNode: adj[node]) {
0 1 3
// unvisited adjacent node
if(dfs(adjacentNode, node, vis, adj))
return true;
0 1 2 3
} vis 1 1 2 0
}
0 1 2 3
vis[node] = 2;
return false; adj 1 2
}
3 3
281
Problem 2 : Course Schedule (continue)
bool dfs(int node, int parent,vector<int> &vis, vector<int> adj[]) {
1
if(vis[node] == 1) return true; -1 0 2
if(vis[node] == 0){
vis[node] = 1;
for(auto adjacentNode: adj[node]) {
0 1 3
// unvisited adjacent node
if(dfs(adjacentNode, node, vis, adj))
return true;
0 1 2 3
} vis 1 1 2 0
}
0 1 2 3
vis[node] = 2;
return false; adj 1 2
}
3 3
282
Problem 2 : Course Schedule (continue)
bool dfs(int node, int parent,vector<int> &vis, vector<int> adj[]) {
1
if(vis[node] == 1) return true; -1 0 2
if(vis[node] == 0){
vis[node] = 1;
for(auto adjacentNode: adj[node]) {
0 1 3
// unvisited adjacent node
if(dfs(adjacentNode, node, vis, adj))
return true;
0 1 2 3
} vis 1 1 2 0
}
0 1 2 3
vis[node] = 2;
return false; adj 1 2
}
3 3
283