题解 | #实现二叉树先序,中序和后序遍历#

实现二叉树先序,中序和后序遍历

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,
};
全部评论

相关推荐

点赞 评论 收藏
分享
码农索隆:想看offer细节
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
昨天 18:13
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务