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实现
最新推荐文章于 2023-10-09 17:13:02 发布