#### 1. Preorder traversal of binary tree (easy)

Preorder traversal is the first access of the head node, followed by the left child node and the right child node of the head node; Postorder traversal is left, right and head nodes; Middle order traversal is left, head node and right;

So as long as you understand one question, three questions are one question

//Here is the official constructor of binary tree in js (only write it once) /** * 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) * } */

var preorderTraversal = function(root) { let res = []; let helper = function(root){ if(root === null) return;//If it is a leaf node, return directly res.push(root.val);//1. Press the root node in helper(root.left);//2. Recursively traverse the left subtree for the purpose of - > pressing the head node of the left subtree of the root node helper(root.right);//3. Recursively traverse the right subtree for the purpose of - > pressing the head node of the right subtree of the root node } helper(root); return res; };

#### 2. Middle order traversal of binary tree (easy)

var preorderTraversal = function(root) { let res = []; let helper = function(root){ if(root === null) return;//If it is a leaf node, return directly helper(root.left);//1. Recursively traverse the left subtree res.push(root.val);//2. Press the root node in helper(root.right);//3. Recursively traverse the right subtree } helper(root); return res; };

#### 3. Post order traversal of binary tree (easy)

var preorderTraversal = function(root) { let res = []; let helper = function(root){ if(root === null) return;//If it is a leaf node, return directly helper(root.left);//1. Recursively traverse the left subtree helper(root.right);//2. Recursively traverse the right subtree res.push(root.val);//3. Press the root node in } helper(root); return res; };

#### 4. Sequence traversal of binary tree (medium)

First, give the official title description of Li Kou ↓↓↓↓↓

Sequence traversal is to traverse the first layer (root node), then the second layer, and then the third layer according to the structure of binary tree... (look at the picture and draw by hand)

Here is my diagram

That is, the output requirements of the result are: first, the nodes from left to right in the first layer, then the second layer, the third layer... How to do this? Obviously, you can't complete the previous recursion, because your recursion traverses deep, extends vertically, and can't move horizontally!

We need to use a data structure, queue. The queue is a first in, first out table. You think, every time you traverse, first press the node into the queue. As long as there is something in the queue, you will traverse the one in the queue first. If there is nothing left, I will then traverse the one in the back, press the one in the back into the queue, and cycle until there is no node. See the detailed notes on the code

##### 1. Sequence traversal I

var levelOrder = function(root) { let res= []; //Define a queue to save nodes according to hierarchical order let queue = [root]; if(root === null) return res; while(queue.length > 0){ //Pop up one node from the queue at a time and put it into res let node = queue.shift(); res.push(node.val); //Check whether the pop-up nodes have children. If so, they will be added to the queue, so that the order of the nodes at the next level can be arranged clearly if(node.left !== null) queue.push(node.left); if(node.right !== null) queue.push(node.right); } return res; };

After reading the notes, if you still don't understand, take a look at my picture

##### 2. Sequence traversal I (easy) (I don't know why this is easy, because the variant I has been made)

Look at the official title description ↓↓↓↓↓

var levelOrder = function(root) { let res = [];//Store the final traversal results //Define a queue. js can use arrays (shift and push methods) to imitate a queue let queue = []; //First, in the first layer, put the root node into the queue. Note that there is already something in the queue if(root !== null) queue.push(root); //Traverse the nodes in the queue while(queue.length > 0){//As long as the length of the queue is greater than 0, it is iterated all the time //Note that the queue length n needs to be defined here, otherwise the queue length will change dynamically let n = queue.length; //Define hierarchy, that is, level is the node that saves each layer let level = []; //n is the maximum number of nodes in the current layer for(let i = 0;i < n;i ++){ //Take a node from the queue header let node = queue.shift(); //Press the value of the extracted node into the current layer level level.push(node.val); //See if there are left and right children in this node. If so, press them into the queue //Therefore, there must be an n above. The number of nodes in each layer should be determined, and the number of nodes in the next layer will also be determined after traversing this layer if(node.left) queue.push(node.left); if(node.right) queue.push(node.right); } //Press the nodes of the current layer into the result as a whole res.push(level); } return res; };

##### 3. Sequence traversal I (zigzag sequence traversal)

Official Title Description ↓↓↓↓↓

In fact, Variant 3 is a small change of variant 2. The positions of the nodes pressed into the current layer are different each time. Odd numbers are inserted from the end of the queue, even numbers are inserted from the head of the queue, and the queue is first in first out at the head (general). Therefore, the order of getting out of the queue will be different according to odd and even

var levelOrder = function(root) { let count = 0; let ans = []; let res = []; let queue = [root]; if(root === null) return res; while(queue.length > 0){ count ++;//Control the number of layers. After traversing one layer, add one to the number of layers level = []; let len = queue.length; for(let i = 0;i < len;i ++){ let node = queue.shift(); //Even layers are added to the queue from the head, so that the out queue is the last in first out queue //Remember that the unshift() method adds elements before the first element of the array, that is, the newly added element is the first element of the array if(count % 2 === 0) level.unshift(node.val); //Odd layers are pushed in from the end of the queue, so that the out of the queue is the normal order, from left to right else level.push(node.val); if(node.left !== null) queue.push(node.left); if(node.right !== null) queue.push(node.right); } res.push(level); } return res; };

ok, that's all for today. Thanks for browsing. The code is not easy. If you don't mind, give me a key three times before you go (◍◍◍ﾉﾞ)