https://www.nowcoder.com/practice/a9fec6c46a684ad5a3abd4e365a9d362?tpId=188&&tqId=38560&rp=1&ru=/activity/oj&qru=/ta/job-code-high-week/question-ranking
/* * function TreeNode(x) { * this.val = x; * this.left = null; * this.right = null; * } */ /** * * @param root TreeNode类 the root of binary tree * @return int整型二维数组 */ function threeOrders( root ) { let pre = [];//前序遍历后的结果 let mid = [];//中序遍历后的结果 let after = [];//后续遍历后的结果 let res = [];//结果数组 //1、前序遍历函数:根前 let preOrder = function(root) { if(root === null) { return []; } pre.push(root.val); preOrder(root.left); preOrder(root.right); } //2、中序遍历函数:根中 let midOrder = function(root) { if(root === null) { return []; } midOrder(root.left); mid.push(root.val); midOrder(root.right); } //3、后续遍历函数:根后 let afterOrder = function(root) { if(root === null) { return []; } afterOrder(root.left); afterOrder(root.right); after.push(root.val); } //4、遍历函数调用 preOrder(root); midOrder(root); afterOrder(root); //5、返回结果数组 res.push(pre, mid, after); return res; } module.exports = { threeOrders : threeOrders };