首頁  >  問答  >  主體

javascript - console.log問題,交換二元樹左右節點,交換前後輸出相同結果

程式碼如下:

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

控制台為什麼都輸出交換後的結果?求解

ringa_leeringa_lee2694 天前895

全部回覆(1)我來回復

  • 给我你的怀抱

    给我你的怀抱2017-06-28 09:29:45

    應該是你看錯了… 你用 console.log(JSON.stringify(F)); 看看

    回覆
    0
  • 取消回覆