javascript - console.log问题,交换二叉树左右节点,交换前后输出相同结果
ringa_lee
ringa_lee 2017-06-28 09:27:45
0
1
792

代码如下:

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_lee
ringa_lee

ringa_lee

全部回复(1)
给我你的怀抱

应该是你看错了…… 你用 console.log(JSON.stringify(F)); 看看

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!