Leetcode 0211. Design Add and Search Words Data Structure

Posted on Tue 05 November 2024 in Leetcode • Tagged with trie-prefix-tree, dfs, recursion

Question Link : https://leetcode.com/problems/design-add-and-search-words-data-structure/

Difficulty: Medium

Premium: False

Question

Design a data structure that supports adding new words and finding if a string matches any previously added string. Implement the WordDictionary class:

WordDictionary() Initializes the object. void addWord(word) Adds word to the data structure, it can …


Continue reading

Leetcode 0623. Add One Row to Tree

Posted on Tue 05 November 2024 in Leetcode • Tagged with binary-tree-manipulation, dfs

Question Link : https://leetcode.com/problems/add-one-row-to-tree/

Difficulty: Medium

Premium: False

Question

Given the root of a binary tree and two integers val and depth, add a row of nodes with value val at the given depth depth. Note that the root node is at depth 1. The adding rule …


Continue reading

Leetcode 0501. Find Mode in Binary Search Tree

Posted on Sun 22 September 2024 in Leetcode • Tagged with bst, binary-tree-traversal-inorder, dfs

Question Link : https://leetcode.com/problems/find-mode-in-binary-search-tree/

Difficulty: Easy

Premium: False

Question

Given the root of a binary search tree (BST) with duplicates, return all the mode(s) (i.e., the most frequently occurred element) in it. If the tree has more than one mode, return them in any order …


Continue reading

Leetcode 0133. Clone Graph

Posted on Tue 11 April 2023 in Leetcode • Tagged with dfs, bfs

Question Link : https://leetcode.com/problems/clone-graph/

Difficulty: Medium

Premium: False

Question

Given a reference of a node in a connected undirected graph. Return a deep copy (clone) of the graph. Each node in the graph contains a value (int) and a list (List[Node]) of its neighbors.

class Node …


Continue reading

Leetcode 1466. Reorder Routes to Make All Paths Lead to the City Zero

Posted on Thu 23 March 2023 in Leetcode • Tagged with dfs, bfs

Question Link : https://leetcode.com/problems/reorder-routes-to-make-all-paths-lead-to-the-city-zero/

Difficulty: Medium

Premium: False

Question

There are n cities numbered from 0 to n - 1 and n - 1 roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport …


Continue reading

Leetcode 2492. Minimum Score of a Path Between Two Cities

Posted on Tue 21 March 2023 in Leetcode • Tagged with dfs, bfs, union-find

Question Link : https://leetcode.com/problems/minimum-score-of-a-path-between-two-cities/

Difficulty: Medium

Premium: False

Question

You are given a positive integer n representing n cities numbered from 1 to n. You are also given a 2D array roads where roads[i] = [ai, bi, distancei] indicates that there is a bidirectional road between cities …


Continue reading

Leetcode 0653. Two Sum IV - Input is a BST

Posted on Sat 08 October 2022 in Leetcode • Tagged with k-sum, dfs

Question Link : https://leetcode.com/problems/two-sum-iv-input-is-a-bst/

Difficulty: Easy

Question

Given the root of a Binary Search Tree and a target number k, return true if there exist two elements in the BST such that their sum is equal to the given target.   Example 1:

Input: root = [5,3,6 …


Continue reading