0% found this document useful (0 votes)
10 views3 pages

Tress&Graphs

The document outlines various tree-based and graph-based problems, including descriptions and test cases for each problem. Key problems include finding the maximum depth of a binary tree, validating a binary search tree, counting islands in a grid, and determining if a graph forms a valid tree. Each problem is accompanied by specific input and expected output examples.

Uploaded by

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

Tress&Graphs

The document outlines various tree-based and graph-based problems, including descriptions and test cases for each problem. Key problems include finding the maximum depth of a binary tree, validating a binary search tree, counting islands in a grid, and determining if a graph forms a valid tree. Each problem is accompanied by specific input and expected output examples.

Uploaded by

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

Tree-Based Problems

1. Maximum Depth of Binary Tree


Description: Find the maximum depth of a binary tree.
Test Cases: - Input: [3,9,20,null,null,15,7] → Output: 3
- Input: [] → Output: 0

2. Validate Binary Search Tree


Description: Determine if a binary tree is a valid BST.
Test Cases: - Input: [2,1,3] → Output: true
- Input: [5,1,4,null,null,3,6] → Output: false

3. Invert Binary Tree


Description: Invert a binary tree.
Test Cases: - Input: [4,2,7,1,3,6,9] → Output: [4,7,2,9,6,3,1]

4. Symmetric Tree
Description: Check whether a binary tree is a mirror of itself (symmetric around its
center).
Test Cases: - Input: [1,2,2,3,4,4,3] → Output: true
- Input: [1,2,2,null,3,null,3] → Output: false

5. Lowest Common Ancestor (BST)


Description: Find the lowest common ancestor of two nodes in a BST.
Test Cases: - Input: root = [6,2,8,0,4,7,9], p = 2, q = 8 → Output: 6
- Input: root = [6,2,8,0,4,7,9], p = 2, q = 4 → Output: 2
Graph-Based Problems
1. Number of Islands
Description: Given a 2D grid map of ’1’s (land) and ’0’s (water), count the number of
islands.
Test Cases: - Input: grid =
[[“1”,“1”,“1”,“1”,“0”],[“1”,“1”,“0”,“1”,“0”],[“1”,“1”,“0”,“0”,“0”],[“0”,“0”,“0”,“0”,“0”]] → Output: 1
- Input: grid = [[“1”,“1”,“0”,“0”,“0”],[“1”,“1”,“0”,“0”,“0”],[“0”,“0”,“1”,“0”,“0”],[“0”,“0”,“0”,“1”,“1”]]
→ Output: 3

2. Clone Graph
Description: Clone an undirected graph. Each node contains a value and a list of
neighbors.
Test Cases: - Input: Node with value 1 connected to 2 and 4, 2 to 1 and 3, 3 to 2 and 4,
4 to 1 and 3 → Output: Cloned graph with same structure

3. Course Schedule (Topological Sort)


Description: Determine if you can finish all courses given prerequisites (cycle detection
in a directed graph).
Test Cases: - Input: numCourses = 2, prerequisites = [[1,0]] → Output: true
- Input: numCourses = 2, prerequisites = [[1,0],[0,1]] → Output: false

4. Graph Valid Tree


Description: Given n nodes labeled from 0 to n-1 and a list of undirected edges,
determine if these edges make up a valid tree.
Test Cases: - Input: n = 5, edges = [[0,1],[0,2],[0,3],[1,4]] → Output: true
- Input: n = 5, edges = [[0,1],[1,2],[2,3],[1,3],[1,4]] → Output: false

5. Word Ladder
Description: Given two words and a dictionary, find the shortest transformation
sequence from beginWord to endWord.
Test Cases: - Input: beginWord = “hit”, endWord = “cog”, wordList =
[“hot”,“dot”,“dog”,“lot”,“log”,“cog”] → Output: 5
- Input: beginWord = “hit”, endWord = “cog”, wordList = [“hot”,“dot”,“dog”,“lot”,“log”] →
Output: 0

You might also like