0% found this document useful (0 votes)
4 views283 pages

CP-Binary TREEgraph

The document discusses non-linear data structures, specifically tree data structures, their applications, and various types such as binary trees, binary search trees, AVL trees, and heaps. It explains the implementation of trees using linear data structures like arrays and linked lists, along with their advantages and disadvantages. Additionally, it presents a problem related to comparing two binary trees for structural identity and provides a solution using a recursive function.

Uploaded by

hemitrana2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views283 pages

CP-Binary TREEgraph

The document discusses non-linear data structures, specifically tree data structures, their applications, and various types such as binary trees, binary search trees, AVL trees, and heaps. It explains the implementation of trees using linear data structures like arrays and linked lists, along with their advantages and disadvantages. Additionally, it presents a problem related to comparing two binary trees for structural identity and provides a solution using a recursive function.

Uploaded by

hemitrana2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 283

Non linear data structure

Prepared By,
Dr. Nikita Bhatt
Ms. Vaishali Korea
19CE119 Hemit Rana

1
tree

2
Applications: Tree

• File explorer/my computer of mobile/any computer

• Evaluate an expression (i.e., parse).

• Routing table.

• To store the possible moves in a game (ex. Chess).

3
Tree data structure

4
Tree data structure

Nonlinear 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;

TreeNode() : val(0), left(nullptr), right(nullptr) {}

TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}

TreeNode(int x, TreeNode *left, TreeNode right) : val(x), left(left), right(right) {}


};

14
Tree data structure
Preference of LL over array:

It has both dynamic size and ease in insertion and deletion as


advantages.

However,

Random access is not possible with linked lists. Also it includes


extra memory space for each element thereby increasing its space
complexity.

15
Tree data structure
Different types of trees

1. Binary tree

at most two child nodes.

16
Tree data structure
Different types of trees

1. Binary tree

2. Binary search tree

value of the left node is always smaller than the


root node which is always smaller than the right
node.

17
Tree data structure
Different types of trees

1. Binary tree
2. Binary search tree

3. AVL tree

self-balancing binary search tree.


The balancing factor is either 1, -1, or 0.

18
Tree data structure
Different types of trees

1. Binary tree
2. Binary search tree
3. AVL tree

4. heap

A heap is a complete binary tree

19
Tree data structure
Different types of trees complete binary tree

1. Binary tree all the levels except the last level,


2. Binary search tree i.e., leaf node should be
3. AVL tree
completely filled,
4. heap
all the nodes should be left-
justified.

20
Tree data structure
Different types of trees

1. Binary tree
2. Binary search tree
3. AVL tree

4. heap

complete binary tree

all the levels except the last level, i.e., leaf


node should be completely filled,

all the nodes should be left-justified.


It is a complete binary tree

21
Tree data structure
Different types of trees

1. Binary tree
2. Binary search tree
3. AVL tree

4. heap

complete binary tree

all the levels except the last level, i.e., leaf


node should be completely filled,

all the nodes should be left-justified.


not a complete binary tree

22
Tree data structure
Different types of trees Min heap:

1. Binary tree A[Parent(i)] <= A[i]


2. Binary search tree
3. AVL tree

4. heap

23
Tree data structure
Different types of trees Max heap:

1. Binary tree A[Parent(i)] >= A[i]


2. Binary search tree
3. AVL tree

4. heap

24
Tree operations
insert delete search

Binary tree O(h) O(h) O(h)

Binary search tree O(h) O(h) O(h)

Height balanced
O(log2N) O(log2N) O(log2N)
tree (AVL)

Min heap O(log2N) O(log2N) O(h)

Max heap O(log2N) O(log2N) O(h)

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;

TreeNode() : val(0), left(nullptr), right(nullptr) {}

TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}

TreeNode(int x, TreeNode *left, TreeNode right) : val(x), left(left), right(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)

Did you recognize the approach??

Level order traversal


or
preorder traversal

40
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {

if(!p && !q)//if both are null p


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;
}

41
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {

if(!p && !q)//if both are null p


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;
}

42
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {

if(!p && !q)//if both are null p


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;
}

43
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {

if(!p && !q)//if both are null p


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;
}

44
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {

if(!p && !q)//if both are null p


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;
}

45
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {

if(!p && !q)//if both are null


return true; 1
p
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;
1
bool lval = isSameTree(p->left, q->left);
bool rval = isSameTree(p->right, q->right); q
2 3
return lval && rval;
}

46
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {

if(!p && !q)//if both are null


return true; 1
p
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;
1
bool lval = isSameTree(p->left, q->left);
bool rval = isSameTree(p->right, q->right); q
2 3
return lval && rval;
}

47
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {

if(!p && !q)//if both are null


return true; 1
p
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;
1
bool lval = isSameTree(p->left, q->left);
bool rval = isSameTree(p->right, q->right); q
2 3
return lval && rval;
}

48
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {

if(!p && !q)//if both are null


return true; 1
p
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;
1
bool lval = isSameTree(p->left, q->left);
bool rval = isSameTree(p->right, q->right); q
2 3
return lval && rval;
}

49
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {

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
p
if(p->val != q->val)//if value is different NULL
return false;
1
bool lval = isSameTree(p->left, q->left);
bool rval = isSameTree(p->right, q->right);
q 2 3
return lval && rval; NULL
}

50
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {

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
p
if(p->val != q->val)//if value is different NULL
return false;
1
bool lval = isSameTree(p->left, q->left);
bool rval = isSameTree(p->right, q->right);
q 2 3
return lval && rval; NULL
}

51
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {

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
p
if(p->val != q->val)//if value is different NULL
return false;
1
bool lval = isSameTree(p->left, q->left); true
bool rval = isSameTree(p->right, q->right);
q 2 3
return lval && rval; NULL
}

52
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {

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
p
if(p->val != q->val)//if value is different NULL
return false;
1
bool lval = isSameTree(p->left, q->left);
bool rval = isSameTree(p->right, q->right);
2 3
return lval && rval; q
} NULL

53
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {

if(!p && !q)//if both are null


return true; return true 1
if((!p && q) || (p && !q))//if one is null and other is not
return false;
2 3
p
if(p->val != q->val)//if value is different NULL
return false;
1
bool lval = isSameTree(p->left, q->left);
bool rval = isSameTree(p->right, q->right);
2 3
return lval && rval; q
} NULL

54
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {

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
p
if(p->val != q->val)//if value is different NULL
return false;
1
bool lval = isSameTree(p->left, q->left);
bool rval = isSameTree(p->right, q->right); true
2 3
return lval && rval; q
} NULL

55
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {

if(!p && !q)//if both are null


return true; 1
p
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;
1
bool lval = isSameTree(p->left, q->left); q
bool rval = isSameTree(p->right, q->right);
2 3
return lval && rval; true && true
}

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) {

if(!p && !q)//if both are null


return true; 1
p
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;
1
bool lval = isSameTree(p->left, q->left);
bool rval = isSameTree(p->right, q->right); q
2 3
return lval && rval;
}

58
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {

if(!p && !q)//if both are null


return true; 1
p
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;
1
bool lval = isSameTree(p->left, q->left);
q
bool rval = isSameTree(p->right, q->right);
2 3
return lval && rval;
}

59
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {

if(!p && !q)//if both are null


return true; 1
p
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;
1
bool lval = isSameTree(p->left, q->left);
q
bool rval = isSameTree(p->right, q->right);
2 3
return lval && rval;
}

60
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {

if(!p && !q)//if both are null


return true; 1
p
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;
1
bool lval = isSameTree(p->left, q->left);
q
bool rval = isSameTree(p->right, q->right);
2 3
return lval && rval;
}

61
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {

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
p
if(p->val != q->val)//if value is different
NULL
return false;
1
bool lval = isSameTree(p->left, q->left);
bool rval = isSameTree(p->right, q->right);
2 3
return lval && rval; q
} NULL

62
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {

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
p
if(p->val != q->val)//if value is different
NULL
return false;
1
bool lval = isSameTree(p->left, q->left);
bool rval = isSameTree(p->right, q->right);
2 3
return lval && rval; q
} NULL

63
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {

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
p
if(p->val != q->val)//if value is different
NULL
return false;
1
bool lval = isSameTree(p->left, q->left); true
bool rval = isSameTree(p->right, q->right);
2 3
return lval && rval; q
} NULL

64
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {

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
p
if(p->val != q->val)//if value is different
NULL
return false;
1
bool lval = isSameTree(p->left, q->left);
bool rval = isSameTree(p->right, q->right);
2 3
return lval && rval;
q
}
NULL

65
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {

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
p
if(p->val != q->val)//if value is different
NULL
return false;
1
bool lval = isSameTree(p->left, q->left);
bool rval = isSameTree(p->right, q->right);
2 3
return lval && rval;
q
}
NULL

66
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {

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
p
if(p->val != q->val)//if value is different
NULL
return false;
1
bool lval = isSameTree(p->left, q->left);
bool rval = isSameTree(p->right, q->right); true
2 3
return lval && rval;
q
}
NULL

67
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {

if(!p && !q)//if both are null


return true; 1
p
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;
1
bool lval = isSameTree(p->left, q->left); q
bool rval = isSameTree(p->right, q->right);
2 3
return lval && rval; true
}

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) {

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;
1
bool lval = isSameTree(p->left, q->left);
bool rval = isSameTree(p->right, q->right);
2 3
return lval && rval; True && True
}

71
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {

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;
1
bool lval = isSameTree(p->left, q->left);
bool rval = isSameTree(p->right, q->right);
2 3
return lval && rval;
}
Same tree

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

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
return lval && rval;
}

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

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
return lval && rval;
}

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

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
return lval && rval;
}

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

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
return lval && rval;
}

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

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
return lval && rval;
}

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

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
return lval && rval;
}

78
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {

if(!p && !q)//if both are null


return true; 1
p
if((!p && q) || (p && !q))//if one is null and other is not
return false;
2

if(p->val != q->val)//if value is different


return false;
1
bool lval = isSameTree(p->left, q->left);
bool rval = isSameTree(p->right, q->right); q
NULL 2
return lval && rval;
}

79
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {

if(!p && !q)//if both are null q is NULL


return true; p is not NULL 1
p
if((!p && q) || (p && !q))//if one is null and other is not
return false;
2

if(p->val != q->val)//if value is different


return false;
1
bool lval = isSameTree(p->left, q->left);
bool rval = isSameTree(p->right, q->right); q
NULL 2
return lval && rval;
}

80
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {

if(!p && !q)//if both are null


return true; 1
p
if((!p && q) || (p && !q))//if one is null and other is not
return false;
2
q is NULL
if(p->val != q->val)//if value is different p is not NULL
return false;
1
bool lval = isSameTree(p->left, q->left);
bool rval = isSameTree(p->right, q->right); q
NULL 2
return lval && rval;
}

81
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {

if(!p && !q)//if both are null


return true; 1
p
if((!p && q) || (p && !q))//if one is null and other is not 2
return false; Return false

if(p->val != q->val)//if value is different


return false;
1
bool lval = isSameTree(p->left, q->left); q
bool rval = isSameTree(p->right, q->right);
NULL 2
return lval && rval;
}

82
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {

if(!p && !q)//if both are null


return true; 1
p
if((!p && q) || (p && !q))//if one is null and other is not 2
return false;

if(p->val != q->val)//if value is different


return false;
1
bool lval = isSameTree(p->left, q->left); q
bool rval = isSameTree(p->right, q->right);
NULL 2
return lval && rval;
}
Not same tree

83
Problem 1 : Same Tree (continue)
bool isSameTree(TreeNode* p, TreeNode* q) {

if(!p && !q)//if both are null


return true;

if((!p && q) || (p && !q))//if one is null and other is not


return false;

if(p->val != q->val)//if value is different


return false;

bool lval = isSameTree(p->left, q->left);


bool rval = isSameTree(p->right, q->right);

return lval && rval; Time complexity: O(h)


}
Space complexity: O(1)

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

Traversal = root -> right -> left


126
Problem 2: Binary Tree Right View (continue)
void recursion(TreeNode* root,int level,vector<int> &ans){
if(root==NULL)
return;

if(ans.size()==level)
ans.push_back(root->val);

recursion(root->right,level+1,ans);
recursion(root->left,level+1,ans);
}

vector<int> rightSideView(TreeNode* root) {


vector<int> ans;
recursion(root,0,ans);
return 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);
}

vector<int> rightSideView(TreeNode* root) { level 0


vector<int> ans;
recursion(root,0,ans);
return ans;
}
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);
}

vector<int> rightSideView(TreeNode* root) { level 0


vector<int> ans;
recursion(root,0,ans);
return ans;
}
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);
}

vector<int> rightSideView(TreeNode* root) { level 0


vector<int> ans;
recursion(root,0,ans);
return ans;
}
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);
}

vector<int> rightSideView(TreeNode* root) { level 0


vector<int> ans;
recursion(root,0,ans);
return ans;
}
ans : [1 ]

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);
}

vector<int> rightSideView(TreeNode* root) { level 1


vector<int> ans;
recursion(root,0,ans);
return ans;
}
ans : [1 ]

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);
}

vector<int> rightSideView(TreeNode* root) { level 1


vector<int> ans;
recursion(root,0,ans);
return ans;
}
ans : [1 ]

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);
}

vector<int> rightSideView(TreeNode* root) { level 1


vector<int> ans;
recursion(root,0,ans);
return ans;
}
ans : [1 ]

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);
}

vector<int> rightSideView(TreeNode* root) { level 1


vector<int> ans;
recursion(root,0,ans);
return ans;
}
ans : [1,2 ]

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
}

vector<int> rightSideView(TreeNode* root) { level 2


vector<int> ans;
recursion(root,0,ans);
return ans;
}
ans : [1,2 ]

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
}

vector<int> rightSideView(TreeNode* root) { level 2


vector<int> ans;
recursion(root,0,ans);
return ans;
}
ans : [1,2 ]

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);
}

vector<int> rightSideView(TreeNode* root) { level 1


vector<int> ans;
recursion(root,0,ans);
return ans;
}
ans : [1,2 ]

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
}

vector<int> rightSideView(TreeNode* root) { level 2


vector<int> ans;
recursion(root,0,ans);
return ans;
}
ans : [1,2 ]

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
}

vector<int> rightSideView(TreeNode* root) { level 2


vector<int> ans;
recursion(root,0,ans);
return ans;
}
ans : [1,2 ]

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);
}

vector<int> rightSideView(TreeNode* root) { level 1


vector<int> ans;
recursion(root,0,ans);
return ans;
}
ans : [1,2 ]

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);
}

vector<int> rightSideView(TreeNode* root) { level 0


vector<int> ans;
recursion(root,0,ans);
return ans;
}
ans : [1,2 ]

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);
}

vector<int> rightSideView(TreeNode* root) { level 1


vector<int> ans;
recursion(root,0,ans);
return ans;
}
ans : [1,2 ]

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);
}

vector<int> rightSideView(TreeNode* root) { level 1


vector<int> ans;
recursion(root,0,ans);
return ans;
}
ans : [1,2 ]

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);
}

vector<int> rightSideView(TreeNode* root) { level 1


vector<int> ans;
recursion(root,0,ans);
return ans;
}
ans : [1,2 ]

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);
}

vector<int> rightSideView(TreeNode* root) { level 2


vector<int> ans;
recursion(root,0,ans);
return ans;
}
ans : [1,2 ]

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);
}

vector<int> rightSideView(TreeNode* root) { level 2


vector<int> ans;
recursion(root,0,ans);
return ans;
}
ans : [1,2 ]

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);
}

vector<int> rightSideView(TreeNode* root) { level 2


vector<int> ans;
recursion(root,0,ans);
return ans;
}
ans : [1,2 ]

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);
}

vector<int> rightSideView(TreeNode* root) { level 2


vector<int> ans;
recursion(root,0,ans);
return ans;
}
ans : [1,2,5 ]

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);
}

vector<int> rightSideView(TreeNode* root) { level 2


vector<int> ans;
recursion(root,0,ans);
return ans;
}
ans : [1,2,5 ]

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);
}

vector<int> rightSideView(TreeNode* root) { level 2


vector<int> ans;
recursion(root,0,ans);
return ans;
}
ans : [1,2,5 ]

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);
}

vector<int> rightSideView(TreeNode* root) { level 1


vector<int> ans;
recursion(root,0,ans);
return ans;
}
ans : [1,2,5 ]

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);
}

vector<int> rightSideView(TreeNode* root) { level 2


vector<int> ans;
recursion(root,0,ans);
return ans;
}
ans : [1,2,5 ]

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);
}

vector<int> rightSideView(TreeNode* root) { level 2


vector<int> ans;
recursion(root,0,ans);
return ans;
}
ans : [1,2,5 ]

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);
}

vector<int> rightSideView(TreeNode* root) { level 2


vector<int> ans;
recursion(root,0,ans);
return ans;
}
ans : [1,2,5 ]

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);
}

vector<int> rightSideView(TreeNode* root) { level 2


vector<int> ans;
recursion(root,0,ans);
return ans;
}
ans : [1,2,5 ]

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);
}

vector<int> rightSideView(TreeNode* root) { level 2


vector<int> ans;
recursion(root,0,ans);
return ans;
}
ans : [1,2,5 ]

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);
}

vector<int> rightSideView(TreeNode* root) { level 2


vector<int> ans;
recursion(root,0,ans); Function execution
return ans; is ended
}
ans : [1,2,5 ]

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);
}

vector<int> rightSideView(TreeNode* root) { level 2


vector<int> ans;
recursion(root,0,ans);
return ans;
}
ans : [1,2,5 ]

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);
}

vector<int> rightSideView(TreeNode* root) { level 2


vector<int> ans;
recursion(root,0,ans);
return ans;
} ans : [1,2,5 ]
168
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);
}

vector<int> rightSideView(TreeNode* root) { Time complexity:O(n)


vector<int> ans; Space complexity:O(h)
recursion(root,0,ans);
return ans;
} h = height of the tree
169
Problem 2: Binary Tree Right View (continue)

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();

int ans; q.pop();


sum += curr->val;
int maxsum = INT_MIN;
if(curr->left != NULL) q.push(curr->left);
if(root == NULL) return ans;
if(curr->right != NULL) q.push(curr->right);
queue<TreeNode*> q;
}
q.push(root);
if(sum > maxsum){
int level = 1;
ans = level;
maxsum = max(sum,maxsum);
}
level++;
}
return ans;
}
172
Extra problem: boundary traversal of a binary tree
https://www.geeksforgeeks.org/boundary-traversal-of-binary-tree/
 Problem description:
 Given a binary tree, print boundary nodes of the binary tree Anti-Clockwise starting from the root.
 The boundary includes
 left boundary (nodes on left excluding leaf nodes)
 leaves (consist of only the leaf nodes)
 right boundary (nodes on right excluding leaf nodes)

 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;

cout << root->data << " ";

// Print the left boundary in top-down manner.


printBoundaryLeft(root->left);

// Print all leaf nodes


printLeaves(root->left);
printLeaves(root->right);

// Print the right boundary in bottom-up manner


printBoundaryRight(root->right);
}
174
Problem 5: boundary traversal of a binary tree
https://www.geeksforgeeks.org/boundary-traversal-of-binary-tree/
 left boundary in top-down manner.
void printBoundaryLeft(Node* root)
{
if (root == nullptr)
return;

if (root->left) {

// to ensure top down order, print the node


// before calling itself for left subtree
cout << root->data << " ";
printBoundaryLeft(root->left);
}
else if (root->right) {
cout << root->data << " ";
printBoundaryLeft(root->right);
}
// do nothing if it is a leaf node, this way we avoid
// duplicates in output
}
175
Problem 5: boundary traversal of a binary tree
https://www.geeksforgeeks.org/boundary-traversal-of-binary-tree/

 Print leaf nodes.


void printLeaves(Node* root)
{
if (root == nullptr)
return;

printLeaves(root->left);

// Print it if it is a leaf node


if (!(root->left) && !(root->right))
cout << root->data << " ";

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

• Web Search Engines

• Recommendation Systems

• Data Analysis and Visualization

179
Graph data structure

180
Graph data structure

Nonlinear data structure

181
Graph data structure
Nonlinear data structure

182
Graph data structure
Adjacency Matrix

183
Graph data structure
Adjacency Matrix

Time complexity: O(V^2) Removing a vertex: O(V)


Space complexity: O(V^2) Removing an edge: O(1)

184
Graph data structure
Adjacency List

185
Graph data structure
Adjacency List

Time complexity: O(E) Removing a vertex: O(V^2)


Space complexity: O(V+E) Removing an edge: O(V)

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

Directed Acyclic Graph

199
Graph data structure
Graph terminologies

Q. If two vertices are endpoints of the same edge, they are


____________.

 adjacent.

Q. The total number of edges occurring to a vertex in a graph is its


____________.

 degree

200
Graph data structure
Graph terminologies

Q. The __________ of a vertex in a directed graph is the total number


of outgoing edges

 out-degree

Q. whereas the __________ is the total number of incoming edges.

 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.

 If the town judge exists, then:

 The town judge trusts nobody.


 Everybody (except for the town judge) trusts the town judge.
 There is exactly one person that satisfies properties 1 and 2.
 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.

 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:

 1 <= n <= 1000


 0 <= trust.length <= 104
 trust[i].length == 2
 All the pairs of trust are unique.
 ai != bi
 1 <= ai, bi <= n

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. The town judge trusts nobody.

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:

3. There is exactly one person that satisfies properties 1 and 2.

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
}

return -1; first second


outdegree
}

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
}

return -1; first second


outdegree
}

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
}

return -1; first second


outdegree
}

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];

indegree[trusted]++; Now For 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 2
}
4 3
return -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];

indegree[trusted]++; Now For 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 2
}
4 3
return -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;
}
}

if(cycleFlag==0) return true;


return false;
}

251
Problem 2 : Course Schedule (continue)
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
numCourses = 4
if (numCourses == 1 || prerequisites.size()==0) return true;

vector<int> adj[numCourses]; prerequisites =


{0,1}
for(int i=0;i<prerequisites.size();i++){
{0,3}
}
adj[prerequisites[i][0]].push_back( prerequisites[i][1]);
0 2 {1,2}
{1,3}
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;
}
}

if(cycleFlag==0) return true;


return false;
}

252
Problem 2 : Course Schedule (continue)
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
numCourses = 4
if (numCourses == 1 || prerequisites.size()==0) return true;

vector<int> adj[numCourses]; prerequisites =


{0,1}
for(int i=0;i<prerequisites.size();i++){
{0,3}
}
adj[prerequisites[i][0]].push_back( prerequisites[i][1]);
0 2 {1,2}
{1,3}
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;
}
}

if(cycleFlag==0) return true;


return false;
}

253
Problem 2 : Course Schedule (continue)
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
numCourses = 4
if (numCourses == 1 || prerequisites.size()==0) return true;

vector<int> adj[numCourses]; prerequisites =


{0,1}
for(int i=0;i<prerequisites.size();i++){
{0,3}
}
adj[prerequisites[i][0]].push_back( prerequisites[i][1]);
0 2 {1,2}
{1,3}
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; 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] == 1) return true;

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] == 1) return true;

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] == 1) return true; basecase


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
264
Problem 2 : Course Schedule (continue)
bool dfs(int node, int parent,vector<int> &vis, vector<int> adj[]) {

if(vis[node] == 1) return true;

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[]) {

if(vis[node] == 1) return true; -1


0 2
if(vis[node] == 0){

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[]) {

if(vis[node] == 1) return true; -1


0 2
if(vis[node] == 0){

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[]) {

if(vis[node] == 1) return true; -1 0 2


if(vis[node] == 0){

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[]) {

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 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[]) {

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 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[]) {

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 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[]) {

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
272
Problem 2 : Course Schedule (continue)
bool dfs(int node, int parent,vector<int> &vis, vector<int> adj[]) {

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
273
Problem 2 : Course Schedule (continue)
bool dfs(int node, int parent,vector<int> &vis, vector<int> adj[]) {

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
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

You might also like