Questions tagged [binary-tree]
A high-level data structure, made of nodes, each with a maximum of 2 children (left and right). Nodes with no children are called leaves, and two nodes with the same parent are known as siblings.
33 questions
8
votes
5
answers
847
views
Find a fraction's parent in the Stern-Brocot tree
Objective
Given a positive reduced fraction, output its parent in the Stern-Brocot tree. The outputted fraction shall also be reduced.
The Stern-Brocot tree
The Stern-Brocot tree is an infinite-height ...
18
votes
11
answers
6k
views
Check If a Binary Tree is Balanced
For each node in a balanced binary tree, the maximum difference in the heights of the left child subtree and the right child subtree are at most 1.
The height of a binary tree is the distance from the ...
10
votes
8
answers
2k
views
Enumerate all binary trees with n nodes
Given an integer n, enumerate all possible full binary trees with n internal nodes. (Full binary trees have exactly 2 children on every internal node). The tree structure should be output as a pre-...
4
votes
3
answers
6k
views
The Great Battle Conundrum- Who are Alive?
Maximillian is the chief commander of the Great Greek Army and he is leading his forces into a crucial war with Spain.
If all the enemy soldiers stand in a straight line incrementally marked starting ...
11
votes
14
answers
851
views
Increasing permutation trees
For this challenge a "binary tree" is a rooted tree where each node has 0 children (leaf) or 2. The children of a node are unordered, meaning that while you might draw the tree with left ...
20
votes
20
answers
3k
views
Fibonacci trees
Background
Fibonacci trees \$T_n\$ are a sequence of rooted binary trees of height \$n-1\$. They are defined as follows:
\$T_0\$ has no nodes.
\$T_1\$ has a single node (the root).
The root node of \$...
16
votes
8
answers
362
views
Side to side, not up and down
Task
Given a list of nodes representing a binary tree of positive integers serialized depth-first, return a list of nodes representing the same tree serialized breadth-first. To represent an absent ...
20
votes
20
answers
7k
views
Write The Shortest Program to Calculate Height of a Binary Tree
The height of a binary tree is the distance from the root node to the node child that is farthest from the root.
Below is an example:
...
25
votes
16
answers
2k
views
Is this a BST pre-order traversal?
Background
A binary tree is a rooted tree whose every node has at most two children.
A labelled binary tree is a binary tree whose every node is labelled with a positive integer; moreover, all ...
15
votes
5
answers
760
views
Pre-order + post-order to in-order
Task
Given the pre-order and post-order traversals of a full binary tree, return its in-order traversal.
The traversals will be represented as two lists, both containing n distinct positive integers,...
5
votes
8
answers
708
views
Shortest Program To Find Lowest Common Ancestor of Two Nodes in a Binary Tree
Any two separate nodes in a binary tree have a common ancestor, which is the root of a binary tree. The lowest common ancestor(LCA) is thus defined as the node that is furthest from the root and that ...
15
votes
12
answers
3k
views
Create Balanced BST from Sorted List of Integers
Given a unique, sorted list of integers, create a balanced binary-search tree represented as an array without using recursion.
For example:
...
20
votes
9
answers
2k
views
Enumerate binary trees
Binary trees
A binary tree is a tree with nodes of three types:
terminal nodes, which have no children
unary nodes, which have one child each
binary nodes, which have two children each
We can ...
24
votes
7
answers
1k
views
Plant a binary forest!
Inspired by A014486.
Challenge
Given an integer input in base 10, construct a representation for the binary forest corresponding to the input. Representations include, but are not limited to, nested ...
7
votes
1
answer
478
views
Print a non-clashing binary search tree
Brief
Print an ASCII representation of a binary search tree.
You should write your own minimum implementation logic of a BST (node, left, right, insert)
(50,30,70,20,80,40,75,90) gives:
...
9
votes
6
answers
1k
views
Find a binary tree's deepest node
Write a program that takes a binary tree as input, and outputs the deepest node and its depth. If there is a tie, print all involved nodes as well as their depths. Each node is represented as:
...
10
votes
5
answers
982
views
Huffman golfing [duplicate]
Write a filter that converts text from standard input to a representation of its Huffman tree on standard output. In as few characters as possible.
you're free to consider newlines or not
output ...