This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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. | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Find the sum of all left leaves in a given binary tree. | |
Example: | |
3 | |
/ \ | |
9 20 | |
/ \ | |
15 7 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Given a binary tree, return all root-to-leaf paths. | |
Note: A leaf is a node with no children. | |
Example: | |
Input: | |
1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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] | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Invert a binary tree. | |
Example: | |
Input: | |
4 | |
/ \ | |
2 7 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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], |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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: |
NewerOlder