Category: Data Structures

  • Subtree of Another Tree

    Given two trees, check if the second tree is a subtree of the first tree For example, Consider the below trees : As evident from the diagram , the subRoot tree is a subtree of root. The input is : root = [3,4,5,1,2], subRoot = [4,1,2] The output is true Try out the solution here:…

  • Level Order Traversal of a Binary Tree

    Given the root of a binary tree , return the level order traversal of the binary tree . Level order traversal means picking nodes from left to right level by level. For example, For the below tree: Level order traversal is [[3],[9,20],[15,7]] It is a list of lists. Each list represent the nodes at that…

  • Maximum Path Sum in a Binary Tree

    Given a binary tree, find the maximum path sum . A path is a sequence of nodes connected by edges. You can start from any node and go to any node in the tree as long as they are connected by edges. Also the same node cannot be present more than one in the sequence.…

  • Invert Binary Tree

    Given a tree , invert it. That is its left node becomes its right node and this follows for every node in each level of the tree. Similarly for the right node. For example: Given the tree: [4,2,7,1,3,6,9] The output is : [4,7,2,9,6,3,1] Try out the solution here: https://leetcode.com/problems/invert-binary-tree/ Solution: Use DFS or BFS Approach…

  • Same Tree or Not?

    Given the root node of two trees, find if they are the same . For example: The below trees: [1,2,3] and [1,2,3] are the same [1,2] and [1,2,3] are not same. Note that though the given input is two arrays above, you can’t just compare each value of one array with the other array. The…

  • Maximum Depth of a Binary Tree

    Given a tree data structure , write an algorithm to find the maximum depth of the tree. Maximum depth is the length of the path from root node to the most distant leaf of the tree. For example: Given a tree represented by the array: [3,9,20,null,null,15,7] where each three elements in the array represents the…

  • Data Structures & Algorithms in Java – Graphs – Alien Dictionary.

    Problem: You are given a list of English words arranged lexically (as in a dictionary). But not in the order of English alphabets but in the order of an alien language which uses English script. With the given words you need to arrange the letters in the right order of the alien dictionary. For example,…

  • Data Structures & Algorithms in Java – Graphs – Valid Tree

    Problem: Given the number of nodes in a graph and the list of edges in the graph , find if it is a valid tree. For example, Given n = 5 and the edges [[0,1] ,[0,2],[0,3],[3,4] It is a valid tree because there are no cycles in it and every node is connected with an…

  • Data Structures & Algorithms in Java – Longest Consecutive Sequence

    Problem Given an array of numbers, find the length of the longest consecutive sequence For example , Given the input [100,4,200,1,3,2] The longest consecutive sequence is [1,2,3,4] So the length is 4. Notice that the sequence need not be in order and can be scattered across the array. Try out the solution here: Leetcode Solution…

  • Data Structures & Algorithms in Java – Graphs – Number of Islands

    Problem: Given a m * n grid representing a map with the value 1 representing land and the value 0 representing water, find out the number of islands in the grid. That is find out the piece of area containing ‘1’s surrounded on all sides by ‘0’s or the grid boundaries (Assume the entire grid…