DSA - Trees Flashcards

1
Q

What are trees in Data Structure?

A

Trees are non-linear DS

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Where are Trees used?

A

Anywhere hierarchy is present. Like File and Folder structures.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does Binary means in Binary Tree.

A
  • binary is zero and ones, two elements
  • binary in BT means, max to max only two nodes are present only
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why BT is Important?

A
  • Lot of questions asked in interviews
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is Root in BT? What are childrens? What are Leaf Nodes? What is subtree?

A
  • Root: node at top is root node
  • Childrens: sub-nodes of any nodes are childres
  • Leaf Nodes: end notes are root notes
  • Sub tree:
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is Ancestors?

A
  • parent of parent node
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Types of Binary Tree

A
  1. full binary tree
  2. complete BT
  3. perfect BT
  4. balanced BT
    5.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what is full binary tree

A

Either has 0 or 2 childrens

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what is complete binary tree

A
  1. All levels are completely filled except the last level
  2. the last level has all nodes in left as possible
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

perfect BT

A
  • all leaf nodes are at same level
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

balanced BT

A
  • height of tree at max log(n)
  • n is number of nodes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

degenerate tree

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to represent BST in C

A

struct node{
int data;
struct node *left_child;
struct *right_child;
};

//function to create a node
struct node* new_node(int x) {
struct node *temp;
temp = malloc(sizeof(struct node));
temp -> data = x;
temp -> left_child = NULL;
temp -> right_child = NULL;

return temp;
}

// searching operation
struct node* search(struct node * root, int x) {
if (root == NULL || root -> data == x) //if root->data is x then the element is found
return root;
else if (x > root -> data) // x is greater, so we will search the right subtree
return search(root -> right_child, x);
else //x is smaller than the data, so we will search the left subtree
return search(root -> left_child, x);
}

// insertion
struct node* insert(struct node * root, int x) {
//searching for the place to insert
if (root == NULL)
return new_node(x);
else if (x > root -> data) // x is greater. Should be inserted to the right
root -> right_child = insert(root -> right_child, x);
else // x is smaller and should be inserted to left
root -> left_child = insert(root -> left_child, x);
return root;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Why traversal is required?

A
  • To solve any problem on tree, we need to traverse it,
  • Traversing is the starting point
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are Types of traversal in Tree

A
  1. DFS
  2. BFS

DFS is further categorized into three more categories
- in order
- pre order
- post order

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is in-order traversal

A
  • it says go to the extreme left subtree, then apply the logic
    Left => Root => Right
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is Pre-order

A

Root => Left => Right

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is Post Order traversal

A

Left => Right => Root

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is BFS Traversal

A

Breath First Traversal
- write nodes level by level
- from left to right

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Implementation of Pre-order

A
  • Root -> Left -> Right

Algorithm / Steps

  • Code

// Here we print the preorder recursively

// 1. we start from root of the tree
// 2.

void preorder (struct node *root)
{
if (root != NULL)
{
printf (“%d “, root->data);
preorder (root->left);
preorder (root->right);
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

In order Traversal Code

A

void inorder(node){

if node == null
return;

inorder(node->left)
print(node)
inorder(node->right)

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Post order traversal code

A

// this node we passed here, is root node

void postorder(node)
{
if node == null
return;

postorder(node->left)
postorder(node->right)
print(node)

// L Ri Root
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Level Order Traversal of Binary Tree / BFS.
What DS we need?
Write code for it

A

C++ code

  • Queue

// code for level order traversal

class solution{
public:
vector<vector<int>> levelOrder(TreeNode *root) {
if(root == NULL) return ans;</int>

queue<TreeNode*> q;

q.push(root);

while(!q.empty()) {
int size = q.size();

vector<int> level;</int>

for(int i=0; i < size ; i++) {

}
}
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
level order traversal in C | BFS in C
https://www.digitalocean.com/community/tutorials/level-order-traversal-in-a-binary-tree
26
Explain what is iterative pre-order traversal
- we are not going to use recursion here - we will use stack DS -
27
code for iterative pre-order traversal
28
explain what is iterative in-order traversal is
29
when to use iterative traversal techniques
30
iterative post-order traversal using 2 stack
31
iterative post order traversal using one stack
32
13. traversal in one traversal, pre-order, in-order, post-order
33
14. maximum depth in binary tree Q. What is depth in BST
34
by which techniques we can find height / depth of BST
to get depth of BST we can use recursive or level order traversal
35
in this two techniques Recursive and Level order traversal which one is better
in level order traversal we have to use queue Data Structure Time com: O(N)
36
what will be time complexity for recursive method, for finding depth of BST
O (height)
37
38
15. check for balanced binary tree
39
16. diameter of Binary Tree
40
17. Maximum path sum in Binary Tree
41
18. check it two trees are Identical no Not
42
19. Zig-Zag or Spiral Traversal in Binary Tree
43
20. Boundary Traversal in Binary Tree
44
21. Vertical Order Traversal of Binary Tree
45
22. Top View of Binary Tree
46
23. Bottom View of Binary Tree
47
24. Right / Left View of Binary Tree
48
25. check for symmetrical binary tree
49
26. print root to node path in binary tree
50
27. lowest common ancestor in binary tree
51
28. maximum width of binary tree
52
29. children sum property in binary tree
53
30. print all the nodes at a distance of K in binary tree
54
31. minimum time taken to BURN the binary tree from a Node
55
32. count total nodes in COMPLETE Binary Tree
56
33. requirements needed to construct a Unique Binary Tree
57
34. construct a binary tree from pre-order and in-order traversal
58
35. construct binary tree from post-order and in-order
59
36. serialize and de-serialize binary tree
60
37. Morris traversal - pre-order and in-order
61
38. Flatten a Binary Tree to Linked List
62
39. Introduction to Binary Search Tree
63
40. Search in a Binary Search Tree
64
41. ceil in a binary search tree
65
42. floor in BST
66
43. Insert a given node in BST
67
44. delete node in BST
68
45. k-th smallest/largest element in BST
69
46. check if tree is a BST or BT, validate a BST
70
47. LCA in BST
71
48. construct a BST from a pre-order traversal
72
49. in-order successor / predecessor in BST
73
50. BST Iterator
74
51. two sum in BST | check if there exists a pair with Sum K
75
52. recover BST | correct BST with two nodes swapped
76
53. largest BST in Binary Tree
77
Heap [syllabus] what is heap?
78
what is heap sort
79
what are types of graph
80
Traversal of graph
81
82