Skip to content

Instantly share code, notes, and snippets.

View vivanks's full-sized avatar
🏠
Working from home

Vivank Sharma vivanks

🏠
Working from home
View GitHub Profile
@vivanks
vivanks / Binary Tree Tilt.java
Created June 15, 2020 14:09
Given a binary tree, return the tilt of the whole tree.
/*
Given a binary tree, return the tilt of the whole tree.
The tilt of a tree node is defined as the absolute difference between the sum of all left subtree node values and the sum of all right subtree node values. Null node has tilt 0.
The tilt of the whole tree is defined as the sum of all nodes' tilt.
Example:
Input:
1
@vivanks
vivanks / Find Mode in Binary Search Tree.java
Created June 12, 2020 06:15
Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST.
/*
Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST.
Assume a BST is defined as follows:
The left subtree of a node contains only nodes with keys less than or equal to the node's key.
The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
Both the left and right subtrees must also be binary search trees.
@vivanks
vivanks / Sum of Left Leaves.java
Created June 2, 2020 11:17
Find the sum of all left leaves in a given binary tree.
/*
Find the sum of all left leaves in a given binary tree.
Example:
3
/ \
9 20
/ \
15 7
@vivanks
vivanks / Binary Tree Paths.java
Last active June 2, 2020 11:11
Given a binary tree, return all root-to-leaf paths.
/*
Given a binary tree, return all root-to-leaf paths.
Note: A leaf is a node with no children.
Example:
Input:
1
@vivanks
vivanks / Lowest Common Ancestor of a Binary Search Tree.java
Created June 2, 2020 07:31
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
/*
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”
Given binary search tree: root = [6,2,8,0,4,7,9,null,null,3,5]
@vivanks
vivanks / Invert Binary Tree.java
Created June 1, 2020 19:27
Invert a binary tree.
/*
Invert a binary tree.
Example:
Input:
4
/ \
2 7
@vivanks
vivanks / Path Sum
Created June 1, 2020 19:01
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.
/*
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,
5
@vivanks
vivanks / Minimum Depth of Binary Tree.java
Created June 1, 2020 18:35
Given a binary tree, find its minimum depth.
/*
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
Note: A leaf is a node with no children.
Example:
Given binary tree [3,9,20,null,null,15,7],
@vivanks
vivanks / Balanced Binary Tree.java
Created June 1, 2020 18:34
Given a binary tree, determine if it is height-balanced.
/*
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:
@vivanks
vivanks / Convert Sorted Array to Binary Search Tree.java
Created June 1, 2020 18:33
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
/*
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
Example:
Given the sorted array: [-10,-3,0,5,9],
One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST: