Count Univalue Subtrees Problem

Problem Given a binary tree, write a program to count the number of Single Valued Subtrees. A Single Valued Subtree is one in which all the nodes have same value. Expected time complexity is O(n). Example Examples 1: 5 / \ 1 5 / \ \ 5 5 5 ...

Generate Parentheses Problem

Problem Implement an algorithm to print all valid (i.e., properly opened and closed) combinations of n-pairs of parentheses. OR Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Follow up Make sure the returned list of strings are sorted. Examples Example 1: Input: n = 3 Output: ["((()))","(()())","(())()","()(())","()()()"] ...

Inorder Successor in Binary Search Tree

Problem Given a Binary Search tree, find the inorder successor of a node. Definition See the definition here. Examples Example 1: 10 / \ 5 30 / \ 22 35 ...

Inorder Successor in Binary Search Tree Using Parent link

Problem Given a node in a binary search tree, return the next bigger element, also known as the inorder successor. You can assume each node has a parent pointer. Definition See the definition here. Here is gist though: The in-order successor of a node in a BST is the next node in in-order traversal, which, for a given node, is the node with the smallest value larger than the given node’s value. ...

Unique Paths in Grid 1 - Count all paths moving right or down

Problem There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time. Given the two integers m and n, return the number of possible unique paths that the robot can take to reach the bottom-right corner. ...

Kth largest element in Binary Search Tree

Problem Given a Binary Search Tree (BST) and a positive integer k, find the k’th largest element in the Binary Search Tree. Examples Example 1: Input: 2 / \ 1 3 and k = 2 Return : 2 As 2 is the second smallest element in the tree. ...

Construct Binary Tree from Inorder and Level Order Traversal

Problem Given a inorder and level order traversal, construct a binary tree from that. Examples Example 1: 3 / \ 9 20 / \ 15 7 ...

Copy List with Random Pointer Problem

Problem A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. i.e. Return a deep copy of the list. OR A linked list of length n is given such that each node contains an additional random pointer, which could point to any node in the list, or null. Construct a deep copy of the list. The deep copy should consist of exactly n brand new nodes, where each new node has its value set to the value of its corresponding original node. Both the next and random pointer of the new nodes should point to new nodes in the copied list such that the pointers in the original list and copied list represent the same list state. None of the pointers in the new list should point to nodes in the original list. ...

Swap Every Kth Node in a LinkedList Problem

Problem Given a linked list, swap every kth node in that. If at the end of the list remaining nodes are less than k, leave them untouched. Input: A linked list, A number k. Examples Example 1: --- title: Input List with k = 4 --- graph LR A1[1] --> B2[2] --> C3[3] --> D4[4]:::kth_node --> E5[5] --> F6[6] --> G7[7] --> H8[8]:::kth_node --> I9[9] --> J10[10] classDef kth_node fill:#FFA500,stroke:#000,stroke-width:2px; ...

Create linked lists of all the nodes at each depth for a Binary Tree

Problem Given a binary search tree, design an algorithm which creates a linked list of all the nodes at each depth (i.e., if you have a tree with depth D, you’ll have D linked lists) Examples Example 1: Input: 1 / \ 2 3 / \ / \ 4 5 6 7 Output: (1) => NULL (2) => (3) => NULL (4) => (5) => (6) => (7) => NULL ...

This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy