描述
分别按照二叉树先序,中序和后序打印所有的节点。
示例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 ) {
// write code here
let a = [], arr1 = [], arr2 = [], arr3 = [];
pre(root,arr1);
mid(root,arr2);
post(root,arr3);
a.push(arr1, arr2, arr3);
return a;
}
function pre( root, arr ){
if(root){
arr.push(root.val);
pre(root.left, arr);
pre(root.right, arr);
}
}
function mid( root, arr){
if(root){
mid(root.left, arr);
arr.push(root.val);
mid(root.right, arr)
}
}
function post(root, arr){
if(root){
post(root.left, arr);
post(root.right, arr);
arr.push(root.val);
}
}
module.exports = {
threeOrders : threeOrders
};