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
Q

level order traversal in C | BFS in C

A

https://www.digitalocean.com/community/tutorials/level-order-traversal-in-a-binary-tree

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

Explain what is iterative pre-order traversal

A
  • we are not going to use recursion here
  • we will use stack DS

-

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

code for iterative pre-order traversal

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

explain what is iterative in-order traversal is

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

when to use iterative traversal techniques

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

iterative post-order traversal using 2 stack

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

iterative post order traversal using one stack

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q
  1. traversal in one traversal, pre-order, in-order, post-order
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q
  1. maximum depth in binary tree

Q. What is depth in BST

A
34
Q

by which techniques we can find height / depth of BST

A

to get depth of BST we can use recursive or level order traversal

35
Q

in this two techniques
Recursive and
Level order traversal

which one is better

A

in level order traversal we have to use queue Data Structure

Time com: O(N)

36
Q

what will be time complexity for recursive method, for finding depth of BST

A

O (height)

37
Q
A
38
Q
  1. check for balanced binary tree
A
39
Q
  1. diameter of Binary Tree
A
40
Q
  1. Maximum path sum in Binary Tree
A
41
Q
  1. check it two trees are Identical no Not
A
42
Q
  1. Zig-Zag or Spiral Traversal in Binary Tree
A
43
Q
  1. Boundary Traversal in Binary Tree
A
44
Q
  1. Vertical Order Traversal of Binary Tree
A
45
Q
  1. Top View of Binary Tree
A
46
Q
  1. Bottom View of Binary Tree
A
47
Q
  1. Right / Left View of Binary Tree
A
48
Q
  1. check for symmetrical binary tree
A
49
Q
  1. print root to node path in binary tree
A
50
Q
  1. lowest common ancestor in binary tree
A
51
Q
  1. maximum width of binary tree
A
52
Q
  1. children sum property in binary tree
A
53
Q
  1. print all the nodes at a distance of K in binary tree
A
54
Q
  1. minimum time taken to BURN the binary tree from a Node
A
55
Q
  1. count total nodes in COMPLETE Binary Tree
A
56
Q
  1. requirements needed to construct a Unique Binary Tree
A
57
Q
  1. construct a binary tree from pre-order and in-order traversal
A
58
Q
  1. construct binary tree from post-order and in-order
A
59
Q
  1. serialize and de-serialize binary tree
A
60
Q
  1. Morris traversal - pre-order and in-order
A
61
Q
  1. Flatten a Binary Tree to Linked List
A
62
Q
  1. Introduction to Binary Search Tree
A
63
Q
  1. Search in a Binary Search Tree
A
64
Q
  1. ceil in a binary search tree
A
65
Q
  1. floor in BST
A
66
Q
  1. Insert a given node in BST
A
67
Q
  1. delete node in BST
A
68
Q
  1. k-th smallest/largest element in BST
A
69
Q
  1. check if tree is a BST or BT, validate a BST
A
70
Q
  1. LCA in BST
A
71
Q
  1. construct a BST from a pre-order traversal
A
72
Q
  1. in-order successor / predecessor in BST
A
73
Q
  1. BST Iterator
A
74
Q
  1. two sum in BST | check if there exists a pair with Sum K
A
75
Q
  1. recover BST | correct BST with two nodes swapped
A
76
Q
  1. largest BST in Binary Tree
A
77
Q

Heap [syllabus]

what is heap?

A
78
Q

what is heap sort

A
79
Q

what are types of graph

A
80
Q

Traversal of graph

A
81
Q
A
82
Q
A