[topic address]

The binary tree is printed from top to bottom layer, with nodes of the same layer printed from left to right, each layer printed to a row.

For example, given a binary tree: [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7
Copy the code

Returns the result of its hierarchical traversal:

[[3], [9,20], [15,7]Copy the code

Tip:

  1. The total number of nodes <= 1000

The result of the preceding traversal is simply placed in a subarray for each level

Simply record the depth of the current node during the preceding traversal, and then place the node value into the corresponding subarray of the result array

When the traversal is complete, return the array of results

The code is as follows:

Var levelOrder = function(root) {const res = []; Function preorder(node,deep){if(node === null) return; if(! res[deep]) res[deep] = []; res[deep].push(node.val); preorder(node.left,deep+1); preorder(node.right,deep+1); } preorder(root,0); Return res; };Copy the code

At this point we are done with leetcode- finger Offer 32-II – printing binary tree II from top to bottom

If you have any questions or suggestions, please leave a comment!