← Back to DSA Course | Chapter 18: Interview Preparation | Lesson 2 of 4

Top Tree और Graph समस्याएँ

Top tree और graph problems trees और maps में चलने के बारे में favorite questions हैं, जैसे सही order में हर node visit करना।

Tree Traversal

Tree traversal किसी tree के हर node को एक specific, useful order में visit करता है — inorder, preorder, और postorder हर एक किसी node को इसके children के सापेक्ष अलग तरीके से visit करते हैं, और सही choice इस पर निर्भर करती है कि problem को असल में उस order से क्या चाहिए।

उदाहरण: Tree Traversal

#include <iostream>
using namespace std;
int val[3] = {2,1,3};
int left[3] = {1,-1,-1}, right[3] = {2,-1,-1};
void inorder(int node) {
	if (node == -1) return;
	inorder(left[node]);
	cout << val[node] << " ";
	inorder(right[node]);
}
int main() { inorder(0); return 0; }
public class Main {
	static int[] val = {2,1,3};
	static int[] left = {1,-1,-1}, right = {2,-1,-1};
	static void inorder(int node) {
		if (node == -1) return;
		inorder(left[node]);
		System.out.print(val[node] + " ");
		inorder(right[node]);
	}
	public static void main(String[] args) { inorder(0); }
}
val = [2,1,3]
left = [1,-1,-1]
right = [2,-1,-1]
def inorder(node):
    if node == -1:
        return
    inorder(left[node])
    print(val[node], end=" ")
    inorder(right[node])
inorder(0)
#include <stdio.h>
int val[3] = {2,1,3};
int left_[3] = {1,-1,-1}, right_[3] = {2,-1,-1};
void inorder(int node) {
	if (node == -1) return;
	inorder(left_[node]);
	printf("%d ", val[node]);
	inorder(right_[node]);
}
int main() { inorder(0); return 0; }

Binary Search Tree

एक Binary Search Tree किसी node के left subtree में हर value को node खुद से छोटा रखता है, और इसके right subtree में हर value बड़ी, जो बिल्कुल वह property है जो search, insert, और delete को एक balanced tree पर O(log n) में चलने देती है।

उदाहरण: Binary Search Tree

#include <iostream>
using namespace std;
int val[3] = {5,3,8};
int left[3] = {1,-1,-1}, right[3] = {2,-1,-1};
bool search(int node, int target) {
	if (node == -1) return false;
	if (val[node] == target) return true;
	return target < val[node] ? search(left[node], target) : search(right[node], target);
}
int main() { cout << "Search 8 in BST: " << (search(0, 8) ? "found" : "not found"); return 0; }
public class Main {
	static int[] val = {5,3,8};
	static int[] left = {1,-1,-1}, right = {2,-1,-1};
	static boolean search(int node, int target) {
		if (node == -1) return false;
		if (val[node] == target) return true;
		return target < val[node] ? search(left[node], target) : search(right[node], target);
	}
	public static void main(String[] args) { System.out.println("Search 8 in BST: " + (search(0, 8) ? "found" : "not found")); }
}
val = [5,3,8]
left = [1,-1,-1]
right = [2,-1,-1]
def search(node, target):
    if node == -1:
        return False
    if val[node] == target:
        return True
    return search(left[node], target) if target < val[node] else search(right[node], target)
print("Search 8 in BST:", "found" if search(0, 8) else "not found")
#include <stdio.h>
int val[3] = {5,3,8};
int left_[3] = {1,-1,-1}, right_[3] = {2,-1,-1};
int search(int node, int target) {
	if (node == -1) return 0;
	if (val[node] == target) return 1;
	return target < val[node] ? search(left_[node], target) : search(right_[node], target);
}
int main() { printf("Search 8 in BST: %s", search(0, 8) ? "found" : "not found"); return 0; }

Graph BFS

Breadth-First Search किसी graph को एक समय में एक पूरी level explore करता है, अगली level पर जाने से पहले current level का हर neighbor visit करते हुए, जो इसे unweighted graph में shortest path ढूंढने के लिए natural choice बनाता है।

उदाहरण: Graph BFS

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() {
	vector<vector<int>> adj = {{1,2},{0,3},{0,3},{1,2}};
	queue<int> q; vector<bool> visited(4,false);
	q.push(0); visited[0]=true;
	while (!q.empty()) {
		int u = q.front(); q.pop(); cout << u << " ";
		for (int v : adj[u]) if (!visited[v]) { visited[v]=true; q.push(v); }
	}
	return 0;
}
import java.util.*;
public class Main {
	public static void main(String[] args) {
		List<List<Integer>> adj = Arrays.asList(Arrays.asList(1,2), Arrays.asList(0,3), Arrays.asList(0,3), Arrays.asList(1,2));
		Queue<Integer> q = new LinkedList<>();
		boolean[] visited = new boolean[4];
		q.add(0); visited[0] = true;
		while (!q.isEmpty()) {
			int u = q.poll(); System.out.print(u + " ");
			for (int v : adj.get(u)) if (!visited[v]) { visited[v] = true; q.add(v); }
		}
	}
}
from collections import deque
adj = [[1,2],[0,3],[0,3],[1,2]]
visited = [False]*4
q = deque([0]); visited[0] = True
while q:
    u = q.popleft()
    print(u, end=" ")
    for v in adj[u]:
        if not visited[v]:
            visited[v] = True
            q.append(v)
#include <stdio.h>
int main() {
	int adj[4][2] = {{1,2},{0,3},{0,3},{1,2}};
	int queue[4], front=0, back=0, visited[4]={0};
	queue[back++]=0; visited[0]=1;
	while (front < back) {
		int u = queue[front++];
		printf("%d ", u);
		for (int i = 0; i < 2; i++) { int v = adj[u][i]; if (!visited[v]) { visited[v]=1; queue[back++]=v; } }
	}
	return 0;
}

Graph DFS

Depth-First Search alternatives try करने के लिए backtrack करने से पहले एक path को जितना गहरा जा सके उतना commit करता है, जो इसे हर possibility explore करने, cycles detect करने, या shortest distances के बजाय connected components ढूंढने के लिए well-suited बनाता है।

उदाहरण: Graph DFS

#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> adj = {{1,2},{0,3},{0,3},{1,2}};
bool visited[4] = {false};
void dfs(int u) { visited[u] = true; cout << u << " "; for (int v : adj[u]) if (!visited[v]) dfs(v); }
int main() { dfs(0); return 0; }
import java.util.*;
public class Main {
	static List<List<Integer>> adj = Arrays.asList(Arrays.asList(1,2), Arrays.asList(0,3), Arrays.asList(0,3), Arrays.asList(1,2));
	static boolean[] visited = new boolean[4];
	static void dfs(int u) { visited[u] = true; System.out.print(u + " "); for (int v : adj.get(u)) if (!visited[v]) dfs(v); }
	public static void main(String[] args) { dfs(0); }
}
adj = [[1,2],[0,3],[0,3],[1,2]]
visited = [False]*4
def dfs(u):
    visited[u] = True
    print(u, end=" ")
    for v in adj[u]:
        if not visited[v]:
            dfs(v)
dfs(0)
#include <stdio.h>
int adj[4][2] = {{1,2},{0,3},{0,3},{1,2}};
int visited[4] = {0};
void dfs(int u) { visited[u] = 1; printf("%d ", u); for (int i = 0; i < 2; i++) if (!visited[adj[u][i]]) dfs(adj[u][i]); }
int main() { dfs(0); return 0; }

Shortest Path

Dijkstra और Bellman-Ford जैसे Shortest-path algorithms एक weighted graph में vertices के बीच minimum-cost route ढूंढते हैं, मुख्य रूप से इसमें अलग हैं कि negative edge weights allowed हैं या नहीं और वे उस extra flexibility के लिए speed कैसे trade करते हैं।

उदाहरण: Shortest Path

#include <iostream>
using namespace std;
int main() {
	cout << "Dijkstra: positive weights only, faster; Bellman-Ford: tolerates negative weights, slower";
	return 0;
}
public class Main {
	public static void main(String[] args) {
		System.out.println("Dijkstra: positive weights only, faster; Bellman-Ford: tolerates negative weights, slower");
	}
}
print("Dijkstra: positive weights only, faster; Bellman-Ford: tolerates negative weights, slower")
#include <stdio.h>
int main() {
	printf("Dijkstra: positive weights only, faster; Bellman-Ford: tolerates negative weights, slower");
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक recursive traversal में nullptr base case skip करना, जो leaves पर crash करता है।
  2. Graph BFS या DFS में visited vertices मार्क न करना, इसलिए cycles हमेशा के लिए loop करते हैं।
  3. सिर्फ parent से compare करके एक BST validate करना बजाय allowed range से।
🔒

Chapter Quiz — Complete all 4 topics to unlock

0/4 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.