版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34629352/article/details/85047945
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
var preorderTraversal = function(root) {
var result = []
var traversal = function(root) {
if (root) {
// 先序
console.log(result.push(root.val));
traversal(root.left);
// 中序
// console.log(root);
traversal(root.right);
// 后序
// console.log(root);
}
return result
};
return traversal(root)
};