4 kyu

Sort binary tree by levels

913 of 14,541kars7e

Description:

You are given a binary tree:

class TreeNode
  attr_accessor :left
  attr_accessor :right
  attr_reader :value
end
data TreeNode a = TreeNode {
  left  :: Maybe (TreeNode a),
  right :: Maybe (TreeNode a),
  value :: a
  } deriving Show
class Node:
    def __init__(self, L, R, n):
        self.left = L
        self.right = R
        self.value = n
class Node {
    Integer value
    Node left
    Node right

    Node(left, right, value) {
        this.value = value
        this.left = left
        this.right = right
    }
}
public class Node
{
    public Node Left;
    public Node Right;
    public int Value;
    
    public Node(Node l, Node r, int v)
    {
        Left = l;
        Right = r;
        Value = v;
    }
}
public class Node {
  public Node left;
  public Node right;
  public int value;
  
  public Node(Node l, Node r, int v) {
    left = l;
    right = r;
    value = v;
  }
}
class Node { 
  constructor(value, left = null, right = null) {
    this.value = value;
    this.left  = left;
    this.right = right;
  }
}
# Available in preloaded
# data Tree a = Leaf | Node a Tree Tree
Leaf = \ leaf _node . leaf
Node = \ v l r . \ _leaf node . node v l r
typedef struct Tree {
    struct Tree *left, *right;
    int value;
} Tree;
struct Node {
  value: u32,
  left: Option<Box<Node>>,
  right: Option<Box<Node>>
}
case class Node(
  left: Option[Node], 
  right: Option[Node],
  value: Int
)

Your task is to return the list with elements from tree sorted by levels, which means the root element goes first, then root children (from left to right) are second and third, and so on.

Return empty array if root is nil.

Example 1 - following tree:

                 2
            8        9
          1  3     4   5

Should return following list:

[2,8,9,1,3,4,5]

Example 2 - following tree:

                 1
            8        4
              3        5
                         7

Should return following list:

[1,8,4,3,5,7]
Trees
Binary Trees
Performance
Algorithms
Sorting

Stats:

CreatedDec 28, 2013
PublishedDec 28, 2013
Warriors Trained36139
Total Skips8459
Total Code Submissions64203
Total Times Completed14541
Ruby Completions913
Haskell Completions658
Python Completions6358
Groovy Completions40
C# Completions1491
Java Completions1945
JavaScript Completions2327
λ Calculus Completions10
C Completions573
Rust Completions549
Scala Completions40
Total Stars959
% of votes with a positive feedback rating93% of 1330
Total "Very Satisfied" Votes1170
Total "Somewhat Satisfied" Votes145
Total "Not Satisfied" Votes15
Ad
Contributors
  • kars7e Avatar
  • jhoffner Avatar
  • xcthulhu Avatar
  • stiell Avatar
  • Unnamed Avatar
  • suic Avatar
  • kazk Avatar
  • JohanWiltink Avatar
  • Blind4Basics Avatar
  • Voile Avatar
  • MattZ306 Avatar
  • FArekkusu Avatar
  • Awesome A.D. Avatar
  • hobovsky Avatar
  • cliffstamp Avatar
  • hensing1 Avatar
  • trashy_incel Avatar
  • user8436785 Avatar
  • akar-0 Avatar
  • Kacarott Avatar
  • XoRMiAS Avatar
  • KayleighWasTaken Avatar
Ad