/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param preStr string字符串 先序遍历序列 * @param midStr string字符串 中序遍历序列 * @return string字符串 */ function getPostOrderOfTree(preStr, midStr) { // write code here let head = new TreeNode(); head.val = preStr[0]; get(head, preStr, midStr); function get(head, pre, mid) { if (pre.length == 0 || mid.length == 0) return; if (pre.length == 1 || mid.length == 1) return; let m = 0; for (let i = 0; i < mid.length; i++) { if (mid[i] == pre[0]) { m = i; break; } } let midLeft = mid.substring(0, m); let midRight = mid.substring(m + 1); let preLeft = pre.substring(1, midLeft.length + 1); let preRight = pre.substring(midLeft.length + 1); let left = null; let right = null; if (preLeft.length > 0) left = new TreeNode(preLeft[0]); if (preRight.length > 0) right = new TreeNode(preRight[0]); head.left = left; head.right = right; get(left, preLeft, midLeft); get(right, preRight, midRight); } let result = []; bl(head, result); console.log(result.join("")); } function TreeNode(val, left, right) { this.val = val; this.left = left == undefined ? null : left; this.right = right == undefined ? null : right; } function bl(head, arr) { if (head == null) return; bl(head.left, arr); bl(head.right, arr); arr.push(head.val); } module.exports = { getPostOrderOfTree: getPostOrderOfTree }; // getPostOrderOfTree("ACDEFHGB", "DECAHFBG")
1 1

相关推荐

求个付费实习岗位:这种就是吃满时代红利又没啥技术水平,只能靠压力学生彰显优越感的老登,别太在意了
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务