描述
分别按照二叉树先序,中序和后序打印所有的节点。
示例1
输入:
{1,2,3}
复制返回值:
[[1,2,3],[2,1,3],[2,3,1]]
代码
/*
* 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 ) {
const preRes = []
function preOrder(root) {
if (root) {
preRes.push(root.val)
preOrder(root.left)
preOrder(root.right)
}
}
const inRes = []
function inOrder(root) {
if (root) {
inOrder(root.left)
inRes.push(root.val)
inOrder(root.right)
}
}
const postRes = []
function postOrder(root) {
if(root) {
postOrder(root.left)
postOrder(root.right)
postRes.push(root.val)
}
}
// write code here
preOrder(root)
inOrder(root)
postOrder(root)
return [preRes, inRes, postRes]
}
module.exports = {
threeOrders : threeOrders
};
运行环境:Javascrip V8
运行时间:51ms
占用内存:7792KB
|