javascript - console.log problem, swap the left and right nodes of the binary tree, and output the same result before and after the swap
ringa_lee
ringa_lee 2017-06-28 09:27:45
0
1
930

code show as below:

class Tree {
    constructor(left=null, right=null){
        this.v = id++;
        this.left = left;
        this.right = right;
    }

    switch() {
        if(null != this.left || null != this.right){
            let temp = this.right;
            this.right = this.left;
            this.left = temp;
        }
        if (null != this.left) {
            this.left.switch();
        }
        if (null != this.right) {
            this.right.switch();
        }
    }
}

var id = 0;

var A = new Tree();
var B = new Tree();
var C = new Tree(A, B);

var D = new Tree();
var E = new Tree(D);

var F = new Tree(C, E);
console.log(F);
F.switch();
console.log(F);

Why does the console output the exchanged results? Solve

ringa_lee
ringa_lee

ringa_lee

reply all(1)
给我你的怀抱

You may have read it wrong... Use console.log(JSON.stringify(F)); to see

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template