10min
The order of pre-order traversal of a binary tree is center-left-right
First traverse all root nodes and left nodes, then process the right node
It can be seen that this is a recursive behavior. Recursive problems can be simplified using stacks.
Iterative solution
var preorderTraversal = function(root) {
if (!root) return null var stack = [root]
var res = []
while(stack.length) {
var node = stack.pop()
res.push(node.val)
node.right && stack.push(node.right)
node.left && stack.push(node.left)
}
return res
};Recursive solution
Recursive problems can of course be solved recursively
var preorderTraversal = function(root) {
var res = []
if (!root) return res
var travel = function (node) {
res.push(node.val)
node.left && travel(node.left)
node.right && travel(node.right)
}
travel(root)
return res
};10min
The order of pre-order traversal of a binary tree is center-left-right
First traverse all root nodes and left nodes, then process the right node
It can be seen that this is a recursive behavior. Recursive problems can be simplified using stacks.
Iterative solution
var preorderTraversal = function(root) {
if (!root) return null var stack = [root]
var res = []
while(stack.length) {
var node = stack.pop()
res.push(node.val)
node.right && stack.push(node.right)
node.left && stack.push(node.left)
}
return res
};Recursive solution
Recursive problems can of course be solved recursively
var preorderTraversal = function(root) {
var res = []
if (!root) return res
var travel = function (node) {
res.push(node.val)
node.left && travel(node.left)
node.right && travel(node.right)
}
travel(root)
return res
};