[B] [C] [D]

Given a binary tree, return a bottom-up traversal of its node values. (that is, from the layer where the leaf node is located to the layer where the root node is located, layer by layer from left to right)

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

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

Return its bottom-up sequence traversal as:

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

The binary tree traversal process is a binary tree traversal process. The binary tree traversal process is a binary tree traversal process.

  1. Create the result array
  2. In the recursive traversal process of ordinary binary tree, the depth of the corresponding node is passed in, and the node value is inserted into the corresponding subarray of the result array according to the depth of the current node
  3. Return an array of results

In this case, we need to complete the order traversal from bottom to top. After completing the order traversal from top to bottom, we need to flip the result array.

The code is as follows:

Var levelOrderBottom = function(root) {const res = []; Function preorder(node,deep){if(node === null) return; // If the current depth subarray is not created, create a subarray if(! res[deep]) res[deep] = []; // Insert the node value into the corresponding subarray res[deep].push(node.val); Preorder (node. Left,deep+1); preorder(node.right,deep+1); } // call preorder(root,0); Return res.reverse(); };Copy the code

At this point we have completed the sequence traversal II of leetcode-107-binary tree

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