diff --git a/core/lib/Drupal/Core/Menu/MenuTreeStorage.php b/core/lib/Drupal/Core/Menu/MenuTreeStorage.php
index f6578fe..b520d93 100644
--- a/core/lib/Drupal/Core/Menu/MenuTreeStorage.php
+++ b/core/lib/Drupal/Core/Menu/MenuTreeStorage.php
@@ -1128,6 +1128,9 @@ protected function doBuildTreeData(array $links, array $parents = [], $depth = 1
   protected function treeDataRecursive(array &$links, array $parents, $depth) {
     $tree = [];
     while ($tree_link_definition = array_pop($links)) {
+      if ((int) $tree_link_definition['depth'] !== $depth) {
+        continue;
+      }
       $tree[$tree_link_definition['id']] = [
         'definition' => $this->prepareLink($tree_link_definition, TRUE),
         'has_children' => $tree_link_definition['has_children'],
