You are using an outdated browser. Please update your browser for a better user experience.

Binary Tree Cameras

trees

Given a binary tree, we install cameras on nodes of the tree.

The camera on a node can monitor it's parent, itself, and any immediate children

Find the minimum number of cameras to be installed in a given binary tree such that each node is monitored.

Example 1:

Input: [0,0,null,0,0]...

Binary Tree Inorder Traversal

trees


Given the root of a binary tree, return the inorder traversal of its nodes' values.

Example 1:

Input: root = [1,null,2,3] Output: [1,3,2]

Example 2:

Input: root = [] Output: [] Example 3:

Input: root = [1] Output: [1]

Example 4:

Input: root = [1,2] Output: [2,1]

Example 5:

Input:...

Binary Tree Tilt

trees

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 node...

BST : Contains(root, int)

trees

Contains(root, int): Given a binary search tree and an integer, determine if the tree contains the key.

...



...

Process

  • Think about the properties of a binary search tree!
  • We begin by looking at the root.
  • If the key is less than the current node, we search the left subtree.
  • If t...

Check Subtree

trees

... T1 and T2 are binary trees, with T1 being bigger than T2. Create an algorithm containsTree(TreeNode T1, TreeNode T2) to determine if T2 is a subtree of T1.

Subtree: A tree T2 is a subtree of T1 if there exists a node n in T1 such that the subtree of n is identical to T2.

Things to think...

Convert Sorted Array to Binary Search Tree

trees

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 1

Given the sorted array: [-1...

First Common Ancestor

trees

Design an algorthim and write code to find the first common ancestor of two nodes in a binary tree. Avoid storing additional nodes in a data structure. NOTE: This is not neccessarily a binary search tree,.

Hints:

  1. How could you leverage the assumption that each node has a link to its parent no...

Last Stone Weight

trees heaps

We have a collection of stones, each stone has a positive integer weight.

Each turn, we choose the two heaviest stones and smash them together. Suppose the stones have weights x and y with x less than or equal to y. The result of this smash is:

If x == y, both stones are totally destroyed;
...

Maximum Path Sum in a Binary Tree

trees

Given a non-empty binary tree, find the maximum path sum.

For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root.

Example 1:

...

Symmetric Tree

trees

boolean isSymmetric(TreeNode root)

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree is symmetric:

    1
   / \
  2   2
 / \ / \
3  4 4  3

But the following is not:

    1
   / \
  2   2
   \   \
   3    3


Hint

...