Skip to content

Instantly share code, notes, and snippets.

/*
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put.
get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
put(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.
The cache is initialized with a positive capacity.
Follow up:
Could you do both operations in O(1) time complexity?
/*
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.
Note: A leaf is a node with no children.
Example:
Given the below binary tree and sum = 22,
5
@pankajjindal
pankajjindal / 112_path_sum.cpp
Last active April 20, 2020 05:29
112_path_sum.cpp
/*
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
Note: A leaf is a node with no children.
Example:
Given the below binary tree and sum = 22,
/*Write a function that takes an unsigned integer and return the number of '1' bits it has (also known as the Hamming weight).
Example 1:
Input: 00000000000000000000000000001011
Output: 3
Explanation: The input binary string 00000000000000000000000000001011 has a total of three '1' bits.*/
class Solution {
public:
@pankajjindal
pankajjindal / 129_sum_root_leaf_node.cpp
Created April 14, 2020 02:56
129_sum_root_leaf_node.cpp
/*Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.
An example is the root-to-leaf path 1->2->3 which represents the number 123.
Find the total sum of all root-to-leaf numbers.
Note: A leaf is a node with no children.
Example:
@pankajjindal
pankajjindal / 110_balanced_binary_tree.cpp
Created April 14, 2020 02:55
110_balanced_binary_tree.cpp
/* Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as:
a binary tree in which the left and right subtrees of every node differ in height by no more than 1.
Example 1:
Given the following tree [3,9,20,null,null,15,7]:
3
/ \
9 20
@pankajjindal
pankajjindal / 104_max_depth_binary_tree.cpp
Created April 14, 2020 02:53
104_max_depth_binary_tree.cpp
/*Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Note: A leaf is a node with no children.
Example:
Given binary tree [3,9,20,null,null,15,7],
@pankajjindal
pankajjindal / 102_level_order_traversal_binary_tree.cpp
Created April 14, 2020 02:47
102_level_order_traversal_binary_tree
/*Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
For example:
Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its level order traversal as:
@pankajjindal
pankajjindal / 101_symmetric_tree.cpp
Last active April 14, 2020 02:50
101_symmetric_tree.cpp
/*Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
1
/ \
2 2
/ \ / \
3 4 4 3
@pankajjindal
pankajjindal / 101_symmetric_tree.cpp
Created April 14, 2020 02:46
101_symmetric_tree.cpp
/*Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
1
/ \
2 2
/ \ / \
3 4 4 3