问题来自力扣第129题:[https://leetcode.cn/problems/sum-root-to-leaf-numbers/](https://link.segmentfault.com/?enc=sBDsLnJG0flWCe%2B19bV4Zg%3D%3D.%2B3gZ0qIto%2FoWC%2FoIaHO1IDUIl%2BuOPFxWfWbP%2F%2BRW4DHKyZ44K1MhGckOzGGyW178O9DMAYsjpffhwgkL%2Bpcr%2Fg%3D%3D) 但问题并不是要解题,题目我自己提交成功提交了。 ## 我的问题是 下面示例中提供的`root`参数提示是`TreeNode`类型,如何复现这个`TreeNode`类型 /** * Definition for a binary 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) * } */ /** * @param {TreeNode} root * @return {number} */ var sumNumbers = function(root) { }; ## 演示案例 以下采用示129题中的例1进行演示,提示:`root`并非数组 root = [1,2,3] ## 我的尝试 我先将root打印了一遍,如下: var sumNumbers = function(root) { console.log('root', root, root.left, root.length); }; 结果如下: root, [1,2,3], [2], undefined 可以得到以下结论: * `root`参数不是数组,因为它没有数组相关的属性 * 但在`console`打印中,`root`展示为数组格式 * * * 我先试着这样赋值数组,打印结果不对: const root = []; root.val = 1; root.left = 2; root.right = 3; console.log(root); // error, output: [val: 1, left: 2, right: 3] 然后我试着直接按照注释将对象还原,打印结果还是不对: function TreeNode(val, left, right) { this.val = (val===undefined ? 0 : val) this.left = (left===undefined ? null : left) this.right = (right===undefined ? null : right) } l1 = new TreeNode(4) r1 = new TreeNode(5) a = new TreeNode(1, l1, r1); console.log(a); // error, output: TreeNode {val: 1, left: TreeNode, right: TreeNode} 然后尝试把root解构: console.log('object like array:', root); var s = ""; for (var property in root) { s = s + "\n "+property +": " + root[property] ; } console.log(s); 输入如下: object like array: [1,2,3] val: 1 left: [object Object] right: [object Object] ### 问题 请问如何做到将一个对象包装成数组类型?