给定一棵二叉树,分别按照二叉树先序,中序和后序打印所有的节点。
数据范围:
,树上每个节点的val值满足
要求:空间复杂度
,时间复杂度
样例解释:
function threeOrders( root ) {
// write code here
const preorder = (cur, arr = []) => {
if (!cur) return arr
arr.push(cur.val)
preorder(cur.left, arr)
preorder(cur.right, arr)
return arr
}
const inorder = (cur, arr = []) => {
if (!cur) return arr
inorder(cur.left, arr)
arr.push(cur.val)
inorder(cur.right, arr)
return arr
}
const postorder = (cur, arr = []) => {
if (!cur) return arr
postorder(cur.left, arr)
postorder(cur.right, arr)
arr.push(cur.val)
return arr
}
return [preorder(root), inorder(root), postorder(root)]
}