题解 | #实现二叉树先序,中序和后序遍历#
实现二叉树先序,中序和后序遍历
http://www.nowcoder.com/practice/a9fec6c46a684ad5a3abd4e365a9d362
JavaScript Node 模式
直接合在一个数组里面!
/*
* 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
const ans = [[],[],[]]
if(!root){
return ans
}
const dfs = (root, ans) => {
ans[0].push(root.val);
root.left && dfs(root.left, ans);
ans[1].push(root.val);
root.right && dfs(root.right, ans);
ans[2].push(root.val);
};
dfs(root, ans);
return ans;
}
module.exports = {
threeOrders: threeOrders,
};