您的位置:首頁技術文章
文章詳情頁

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

瀏覽:175日期:2023-05-02 08:20:30

問題描述

代碼如下:

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

控制臺為什么都輸出交換后的結果?求解

問題解答

回答1:

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

標簽: JavaScript
国产综合久久一区二区三区