Posts with tag 'tree'

Binary Search Tree Typo

less than 1 minute read

You are given a binary tree root which is almost a binary search tree except two nodes’ values have been swapped. Return the original binary search tree.

Longest Tree Path

less than 1 minute read

Given a binary tree root, return the longest path between any two nodes in the tree.

Top View of a Tree

less than 1 minute read

Given a binary tree root, return the top view of the tree, sorted left-to-right.

Enlarge BST

less than 1 minute read

Given a binary search tree root, replace every node’s value v by its value plus the sum of all other values in the tree that are greater than v.

Split Tree to Maximize Product

less than 1 minute read

Given a binary tree root, consider deleting an edge in the tree so that the tree becomes disjoint with two trees. Then, take the sum of each subtree and mult...

Most Frequent Subtree Sum

less than 1 minute read

Given a binary tree root, find the most frequent subtree sum. The subtree sum of a node is the sum of all values under a node, including the node itself. You...

Lowest Common Ancestor

less than 1 minute read

Given a binary tree root, and integers a and b, find the value of the lowest node that has a and b as descendants. A node can be a descendant of itself.

Largest Tree Sum Path

less than 1 minute read

Given a binary tree root, return the largest sum of any path between any two nodes.

Longest Tree Sum Path From Root to Leaf

less than 1 minute read

Given a binary tree root, return the sum of the longest path from the root to a leaf node. If there are two equally long paths, return the larger sum.

Largest Root to Leaf Sum

less than 1 minute read

Given the root to a binary tree root, return the largest sum of any path that goes from the root to a leaf.

Partition Tree

less than 1 minute read

Given the root to a binary tree root, return a list of two numbers where the first number is the number of leaves in the tree and the second number is the nu...

Longest Even Value Path

less than 1 minute read

Given a binary tree root, return the longest path consisting of even values between any two nodes in the tree.

Vertical Lines in Binary Tree

1 minute read

Given a binary tree root, return the number of unique vertical lines that can be drawn such that every node has a line intersecting it. Each left child is an...

Univalue Tree

less than 1 minute read

Given a binary tree root, return whether all values in the tree are the same.

Univalue Tree Count

less than 1 minute read

A univalue tree is a tree where all nodes under it have the same value.

Twin Trees

1 minute read

Given two binary trees, root0 and root1, return whether their structure and values are equal.

Tree with Distinct Parities

less than 1 minute read

Given a binary tree root, return the number of perfect nodes. A perfect node has two properties:

Tree Traversal

1 minute read

You are given a tree root and a list of strings moves consisting of "RIGHT", "LEFT" and "UP". Starting from root, traverse the tree by performing each move i...

Tree Sum

less than 1 minute read

Given a binary tree root, return the sum of all values in the tree.

Tree Pruning

less than 1 minute read

Given a binary tree root, prune the tree so that subtrees containing all 0s are removed.

Tree From Pre/Inorder Traversals

less than 1 minute read

Given a list of unique integers preorder and another list of unique integers inorder, representing the pre-order and in-order traversals of a binary tree, re...

Tree Coloring

1 minute read

You are given a binary tree root where the value of each node represents its color. In the tree there are at most 2 colors. Return whether it’s possible to s...

Symmetric Binary Tree

less than 1 minute read

Given the root to a binary tree root, return whether it is symmetric.

Swappable Trees

less than 1 minute read

Given two trees root0 and root1, return whether you can transform root0 into root1 by swapping any node’s left and right subtrees any number of times.

Sum Tree

less than 1 minute read

Given a binary tree root, return whether for every node in the tree other than the leaves, its value is equal to the sum of its left child’s value and its ri...

Sum of Two Numbers in BSTs

1 minute read

You are given two binary search trees a and b and an integer target. Return whether there’s a number in a and a number in b such that their sum equals to tar...

Sum of the Deepest Nodes

less than 1 minute read

Given a binary tree root, find the sum of the deepest node values.

Sum of Right Leaves

less than 1 minute read

Given a binary tree root, return the sum of all leaves that are right children.

Sum of Digit Paths in a Tree

less than 1 minute read

You are given a binary tree root with each node containing single digits from 0 to 9. Each path from the root to the leaf represents a number with its digits...

Subtree

less than 1 minute read

You are given two binary trees root, and target. Return whether target is a subtree of root — that is, whether there’s a node in root that is identically sam...

Subtree with Maximum Average

1 minute read

Given a binary tree root, return the maximum average value of a subtree. A subtree is defined to be some node in root including all of its descendants. A sub...

Sibling Tree Value

1 minute read

You are given an integer k and a binary search tree root, where each node is either a leaf or contains 2 children.

Second Place

less than 1 minute read

Given a binary tree root, return the depth of the second deepest leaf. Note that if there are multiple deepest leaves, the second deepest leaf is the next hi...

Search in a Virtually Complete Binary Tree

1 minute read

Consider a complete binary tree of n nodes whose values are 1 to n. The root has value of 1, its left child is 2 and its right child is 3. In general, nodes’...

Search in a Binary Search Tree

less than 1 minute read

Given a binary search tree root and an integer val, determine whether val is in the tree.

Palindromic Tree

less than 1 minute read

Given a binary tree root where each node contains a digit from 0-9, return whether its in-order traversal is a palindrome.

Only Child

less than 1 minute read

Given a binary tree root, return the number of nodes that are an only child. A node x is an only child if its parent has exactly one child (x).

Next Node on Its Right

1 minute read

You are given a binary tree root containing unique values, and an integer target. Find the node with value target and return the node that’s directly right o...

Merging Binary Trees

1 minute read

Given two binary trees node0 and node1, return a merge of the two trees where each value is equal to the sum of the values of the corresponding nodes of the ...

List to Binary Search Tree

less than 1 minute read

Given a sorted list nums of size n, construct a binary search tree by

Level Order Traversal

less than 1 minute read

Given a binary tree root return a level order traversal of the node values.

Level Order Alternating

less than 1 minute read

Given a binary tree root, return values of the nodes in each level, alternating from going left-to-right and right-to-left.

Leftmost Deepest Tree Node

less than 1 minute read

Given a binary tree root, find the value of the deepest node. If there’s more than one deepest node, then return the leftmost one.

Left Side View of a Tree

less than 1 minute read

Given a binary tree root, return the leftmost node’s value on each level of the tree.

Leaves in Same Level

less than 1 minute read

Given a binary tree root, return whether all leaves are at the same level.

Leaf Equivalent Trees

less than 1 minute read

Given two binary trees root0 and root1, return whether the sequence of leaves left-to-right in both trees are the same.

Kth Smallest in a Binary Search Tree

less than 1 minute read

Given a binary search tree root, and k return the kth (0-indexed) smallest value in root. It is guaranteed that the tree has at least k + 1 nodes.

Invert Tree

less than 1 minute read

Given a binary tree root, invert it so that its left subtree and right subtree are swapped and the children are recursively inverted.

Inorder Traversal

less than 1 minute read

Given a binary tree root, return an inorder traversal of root as a list.

Inorder Successor

1 minute read

Given a binary search tree root containing unique values, and an integer t, return the value of the inorder successor of t. That is, return the smallest valu...

Height Balanced Tree

less than 1 minute read

Given the root of a binary tree, return whether its height is balanced. That is, for every node in the tree, the absolute difference of the height of its lef...

Elephant Tree

less than 1 minute read

Given a binary tree root, return the same tree except every node’s value is replaced by its original value plus all of the sums of its left and right subtrees.

Diagonal Tree Traversal

less than 1 minute read

Given a binary tree root, return the sum of each of the diagonals in the tree starting from the top to bottom right.

Delete Even Leaves

less than 1 minute read

Given a binary tree root, repeatedly delete all leaves that have even values. That is, if after deletions, a node becomes a leaf with an even value, it too s...

Cutting Binary Search Tree

less than 1 minute read

Given a binary search tree root, an integer lo, and another an integer hi, remove all nodes that are not between [lo, hi] inclusive.

Counting Maximal Value Roots in Binary Tree

less than 1 minute read

Given a binary tree root, count and return the number of nodes where its value is greater than or equal to the values of all of its descendants.

Count BST Nodes in a Range

less than 1 minute read

Given a binary search tree root, and integers lo and hi, return the count of all nodes in root whose values are between [lo, hi] (inclusive).

Complete Binary Tree

less than 1 minute read

Given a binary tree root, return whether it’s a complete binary tree.

Binary Tree to Linked List

less than 1 minute read

Given a binary tree root, convert it to a singly linked list using an inorder traversal.

Binary Search Tree Validation

less than 1 minute read

Given a binary tree root, return whether it’s a binary search tree. A binary tree node is a binary search tree if :

Back to top ↑