[Algorithm-LeetCode] 429.Hierarchical traversal of N-fork trees (N-fork trees; Hierarchical traversal; BFS)

Keywords: Javascript Algorithm leetcode bfs

429.N-Fork Tree Hierarchical Traversal-Force Button (LeetCode)

Release: September 25, 2021 01:01:25

Problem description and examples

Given an N-fork tree, returns the hierarchical traversal of its node values (that is, from left to right, traverse layer by layer).
The serialized input to the tree is traversed in sequence, with each set of child nodes separated by null values (see example).

Example 1:

Input: root = [1,null,3,2,4,null,5,6]
Output: [[1], [3,2,4], [5,6]]

Example 2:

root = [

Output: [[1], [2,3,4,5], [6,7,8,9,10], [11,12,13], [14]]

Source: LeetCode
Copyright belongs to the withholding network. For commercial reprinting, please contact the official authorization. For non-commercial reprinting, please indicate the source.

Tree height will not exceed 1000
The total number of nodes in the tree is between [0, 10^4]

My Questions

My Question 1 (BFS Hierarchical Traversal)

Okay, this is the same idea as the one I did before about traversing binary trees in sequence:

Reference resources: [Algorithm-LeetCode] 102.Binary Tree Ordered Traverse (Binary Tree; Ordered Traverse; BFS; Generate Binary Tree)_Ryanian's Blog-CSDN Blog
Reference resources: [Algorithm-LeetCode] 199.Right View of Binary Tree (Binary Tree; Hierarchical Traversal; BFS)_Ryanan's Blog-CSDN Blog

The only difference is that the object traversed in this topic is N-fork tree, and the ideas are the same.

So you just need to make a few changes based on the above topic of hierarchical traversal. More detailed descriptions of hierarchical traversal can be reviewed in the blog above and will not be repeated here.

See the notes below for additional details on this topic:

 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 * @param {TreeNode} root
 * @return {number[]}
var levelOrder = function (root) {
  // If the tree is an empty tree, it returns the empty result directly.
  // Don't miss this step or you won't be able to pass all LeetCode test cases
  if(root === null) {
    return [];
  // result stores traversal results for all layers
  let result = [];
  // Initialize queue as auxiliary queue, through the combination of array push and shift methods, the queue FIFO feature can be achieved
  // And queue the root node first, otherwise subsequent while loops will not work
  let queue = [root];
  // If the queue is not empty, it means that not all layers have been traversed. Note that the while loop is iterated on a layer-by-layer basis
  while (queue.length) {
  	// level stores all nodes in the current layer, so it should be initialized to an empty array every time a new layer is entered
    let level = [];
    // This for loop is used to iterate through all nodes in the current layer, noting why I don't write i<queue.length directly below
    for (let i = 0, len = queue.length; i < len; i++) {
      // Eject the first node of the queue, but be careful not to throw it away right away because it's still useful later
      let node = queue.shift();
      // Save the node value just popped up into level
      // The for loop below forces all child nodes that are currently traversing the node (that is, the node that just popped up the queue) into the queue
      // This is the only difference from the simple binary tree that traversed [tag1]
      for(let j = 0; j < node.children.length; j++) {
        if(node.children[j]) {
    // The end of the for loop indicates that all nodes in the current layer have been stored in level and a result has been obtained
  // The end of the while loop indicates that all layers in the tree have been traversed and the results returned
  return result;

Submit Records
38 / 38 Pass test cases
 Status: Passed
 Execution time: 88 ms, At all JavaScript 82 defeated in submission.76%Users
 Memory consumption: 41.7 MB, At all JavaScript 93 defeated in submission.96%Users
 Time: 2021/09/25 01:12	

In general, the while loop above is responsible for traversing the depth direction of the N-fork tree, while the for loop is responsible for traversing the width direction in a layer.

In this for loop, there is another for loop (that is, the one at tag1) to complete the queue operation on the child nodes of the currently traversing node.

The queue is used as a secondary queue FIFO structure to ensure the effect of traversing layer by layer.

The [tag1] identified in the program above is the only difference from the previous sequential traversal problem. Note the contrast.

Official Questions

Update: July 29, 2021 18:43:21

Because I'm concerned about copyright ownership, I no longer paste specific codes in some of the Official Titles, so I can see them in the link below.

Update: September 25, 2021 01:14:07

Reference resources: N-Fork Tree Hierarchical Traversal - N-Fork Tree Hierarchical Traversal - LeetCode

[End of update]


Update: 22:00:41 September 22, 2021
Reference resources: [Algorithm-LeetCode] 102.Binary Tree Ordered Traverse (Binary Tree; Ordered Traverse; BFS; Generate Binary Tree)_Ryanian's Blog-CSDN Blog
Update: September 23, 2021 21:31:44
Reference resources: [Algorithm-LeetCode] 199.Right View of Binary Tree (Binary Tree; Hierarchical Traversal; BFS)_Ryanan's Blog-CSDN Blog
Update: July 30, 2021 21:03:06
Reference resources: [WeChat Public Number: Code Picture 2020-09-25] Binary Tree: Traverse the stage in sequence!
Reference resources: Depth-first traversal (DFS) and breadth-first traversal (BFS)_JeansPocket's blog-CSDN's blog_breadth-first traversal
Reference resources: Summary of three ways to define and initialize a js two-dimensional array _javascript tips_home of scripts

Posted by joshblue on Fri, 24 Sep 2021 09:41:58 -0700