var preorderTraversal = function(root) {
const res = [];
const stack = [];
if(root) stack.push(root);
while(stack.length) {
const n = stack.pop()
res.push(n.val)
if(n.right) stack.push(n.right)
if(n.left) stack.push(n.left)
}
144.二叉树的前序遍历 js实现
最新推荐文章于 2022-07-01 16:53:42 发布