Determine if a binary tree is height-balanced 1

Title: How to determine if a binary tree is height-balanced? Source: www.geeksforgeeks.org A tree where no leaf is much farther away from the root than any other leaf. Different balancing schemes allow different definitions of “much farther” and different amounts of work to keep them balanced. Consider a height-balancing scheme where following conditions should be ...

Rat in a Maze

Title: Rat in a Maze Source: www.geeksforgeeks.org Let us discuss Rat in a Maze as another example problem that can be solved using Backtracking. A Maze is given as N*N binary matrix of blocks where source block is the upper left most block i.e., maze[0][0] and destination block is lower rightmost block i.e., maze[N-1][N-1]. A ...

Check if a binary tree is BST

Title: A program to check if a binary tree is BST or not Source: www.geeksforgeeks.org A binary search tree (BST) is a node based binary tree data structure which has the following properties. • The left subtree of a node contains only nodes with keys less than the node’s key. • The right subtree of ...

Path Sum II

Title: Path Sum II Source: leetcode.com Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum. sum = 22 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1 1234567               5             / \            4   8           /   / \          11  13  4         ...

Game of Life

Title: Game of Life Source: leetcode.com According to the Wikipedia’s article: “The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970.” Given a board with m by n cells, each cell has an initial state live (1) or dead (0). Each cell ...

Word Search

Title: Word Search Source: leetcode.com Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once. For example, Given board ...

Word Pattern 1

Title: Word Pattern Source: leetcode.com Given a pattern and a string str, find if str follows the same pattern. Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str. Examples: pattern = "abba", str = "dog cat cat dog" should return true. ...

Valid Anagram

Title: Valid Anagram Source: leetcode.com Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = “anagram”, t = “nagaram”, return true. s = “rat”, t = “car”, return false. Note: You may assume the string contains only lowercase alphabets. Follow up: What if ...

Unique Paths II

Title: Unique Paths II Source: leetcode.com Follow up for “Unique Paths”: Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the middle of a 3×3 ...

Unique Paths

Title: Unique Paths Source: leetcode.com A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the ...