JavaScript algorithm - binary tree traversal

1. Construct a binary tree

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)
}

Next we need to traverse the following binary tree:
insert image description here
Traversal results:
Preorder: "Center-Left-Right" 0137849256
Inorder: "Left-Center-Right" 7381940526
Post-order: "Left-Right-Center" 7839415620

2. Recursive traversal

Depending on where the recursion is called, there are three types of results.

var preorder = []// 前序结果
var inorder = []// 中序结果
var postorder = []// 后序结果

var loop = function(root){
    
    
	// 当前节点为空,表示达到了叶子节点
    if (root == null) return

    preorder.push(root.val)  // 前序
    loop(root.left)
    inorder.push(root.val)// 中序
    loop(root.right)
    postorder.push(root.val)// 后序
}
loop(root)

3. Non-recursive traversal

3.1 Precedence

  1. The root node is pushed onto the stack, and the top element of the stack is taken out accordingly.
  2. Access the top element of the stack, pop the stack at the same time, take the top element as the current element, push the right node of the current element onto the stack, and push the left node onto the stack (note: 右先入那么右后出).
  3. Repeat the above operations until the entire stack is empty, then the traversal ends.
var preorderTraversal = function(root) {
    
    
    var arr = []
    arr.push(root)
    var res = []
    while (arr.length) {
    
    
        var temp = arr.pop()
        if (!temp) break;
        //当前节点的值放入结果数组
        res.push(temp.val)
        //右子树入栈
        if (temp.right) {
    
    
            arr.push(temp.right)
        }
        //左子树入栈
        if (temp.left) {
    
    
            arr.push(temp.left)
        }
    }
    return res
};

3.2 Inorder

  1. The loop pushes the root node and its left subtree onto the stack.
  2. Until the left subtree is empty, access the top element of the stack, and take the top element of the stack as the current element, and pop it out of the stack.
  3. Start to visit the right subtree, and loop out the stack until the entire stack is empty, then the traversal ends.
var inorderTraversal = function(root) {
    
    
    var res = []
    var arr = []

    while(arr.length || root) {
    
    
        if (root) {
    
    
            arr.push(root)
            root = root.left
        } else {
    
    
            let temp = arr.pop()
            res.push(temp.val)
            root = temp.right
        }
    }

    return res
};

3.3 Postscript

Contrary to preorder traversal.
The first sequence is to use push to add data to the back of the res array, and the second sequence is to use unshift to add data to the front of the array.

var postorderTraversal = function(root) {
    
    
    var arr = []
    arr.push(root)
    var res = []
    while(arr.length) {
    
    
        var temp = arr.pop()
        if (!temp) break
        res.unshift(temp.val)// 从前往后塞入数据
        if(temp.left) {
    
    // 左节点先入栈
            arr.push(temp.left)
        }
        if(temp.right) {
    
    
            arr.push(temp.right)
        }
    }

    return res
};

Guess you like

Origin blog.csdn.net/ZHANGYANG_1109/article/details/127812334