Tree Level Lists

AlgorithmAlgorithmBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left child and the right child. In this challenge, we will create a list for each level of a binary tree.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL algorithm(("`Algorithm`")) -.-> algorithm/BasicAlgorithmsGroup(["`Basic Algorithms`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) python(("`Python`")) -.-> python/ErrorandExceptionHandlingGroup(["`Error and Exception Handling`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) algorithm/BasicAlgorithmsGroup -.-> algorithm/graphs_trees("`Graphs Trees`") algorithm/BasicAlgorithmsGroup -.-> algorithm/recursion_dynamic("`Recursion Dynamic`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/strings("`Strings`") python/BasicConceptsGroup -.-> python/type_conversion("`Type Conversion`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/ControlFlowGroup -.-> python/while_loops("`While Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/default_arguments("`Default Arguments`") python/ObjectOrientedProgrammingGroup -.-> python/classes_objects("`Classes and Objects`") python/ObjectOrientedProgrammingGroup -.-> python/constructor("`Constructor`") python/ObjectOrientedProgrammingGroup -.-> python/polymorphism("`Polymorphism`") python/ObjectOrientedProgrammingGroup -.-> python/encapsulation("`Encapsulation`") python/ErrorandExceptionHandlingGroup -.-> python/raising_exceptions("`Raising Exceptions`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills algorithm/graphs_trees -.-> lab-268838{{"`Tree Level Lists`"}} algorithm/recursion_dynamic -.-> lab-268838{{"`Tree Level Lists`"}} python/variables_data_types -.-> lab-268838{{"`Tree Level Lists`"}} python/strings -.-> lab-268838{{"`Tree Level Lists`"}} python/type_conversion -.-> lab-268838{{"`Tree Level Lists`"}} python/conditional_statements -.-> lab-268838{{"`Tree Level Lists`"}} python/for_loops -.-> lab-268838{{"`Tree Level Lists`"}} python/while_loops -.-> lab-268838{{"`Tree Level Lists`"}} python/lists -.-> lab-268838{{"`Tree Level Lists`"}} python/tuples -.-> lab-268838{{"`Tree Level Lists`"}} python/function_definition -.-> lab-268838{{"`Tree Level Lists`"}} python/default_arguments -.-> lab-268838{{"`Tree Level Lists`"}} python/classes_objects -.-> lab-268838{{"`Tree Level Lists`"}} python/constructor -.-> lab-268838{{"`Tree Level Lists`"}} python/polymorphism -.-> lab-268838{{"`Tree Level Lists`"}} python/encapsulation -.-> lab-268838{{"`Tree Level Lists`"}} python/raising_exceptions -.-> lab-268838{{"`Tree Level Lists`"}} python/data_collections -.-> lab-268838{{"`Tree Level Lists`"}} python/build_in_functions -.-> lab-268838{{"`Tree Level Lists`"}} end

Tree Level Lists

Problem

Given a binary search tree, create a list for each level of the tree. Each list should contain the nodes at that level of the tree. The lists should be returned in an array of arrays, where each subarray represents a level of the tree.

Requirements

To solve this problem, the following requirements must be met:

  • The given tree is a binary search tree.
  • Each level of the tree should be represented by a list of nodes.
  • A Node class with an insert method is already provided.
  • The solution should fit in memory.

Example Usage

For example, given the binary search tree with the following values:

5, 3, 8, 2, 4, 1, 7, 6, 9, 10, 11

The function should return the following array of arrays:

[[5], [3, 8], [2, 4, 7, 9], [1, 6, 10], [11]]

Note that each number in the result is actually a node containing the number.

Summary

In this challenge, we have learned how to create a list for each level of a binary search tree. By meeting the requirements, we can solve this problem and return an array of arrays representing each level of the tree.