144.二叉树的层次遍历(javascript)144.BinaryTreePreorderTraversal

原创
小哥 3年前 (2022-11-10) 阅读数 9 #大杂烩

144. 二叉树的预序遍历(javascript)144. Binary Tree Preorder Traversal

94. 二叉树的中阶遍历。(javascript)94. Binary Tree Inorder Traversal

145. 二叉树的后序遍历(javascript)145. Binary Tree Postorder Traversal

给你二叉树的根节点 root ,返回其节点值 前序 遍历。
Given the root of a binary tree, return the preorder traversal of its nodes’ values.

示例 1:

输入:root = [1,null,2,3]
输出:[1,2,3]

示例 2:

输入:root = []
输出:[]

示例 3:

输入:root = [1]
输出:[1]

示例 4:

输入:root = [1,2]
输出:[1,2]

示例 5:

输入:root = [1,null,2]
输出:[1,2]

var preorderTraversal = function (root) {
    let res = []
    var preorder = function (root) {
        if(root==null){
            return
        }
        res.push(root.val)
        preorder(root.left)
        preorder(root.right)
    };
    preorder(root)
    return res
};

leetcode: https://leetcode-cn.com/problems/binary-tree-preorder-traversal/

版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除