invert-binary-tree

  • 递归
1
2
3
4
5
6
7
8
9
10
var preorderTraversal = function(root, res = []) {
if (!root) {
return res;
}
const { val, left, right } = root;
res.push(val);
preorderTraversal(left, res);
preorderTraversal(right, res);
return res;
};