- Notifications
You must be signed in to change notification settings - Fork24
Open
Labels
Description
前序遍历:先打印当前节点,再打印当前节点的左子树,最后打印当前节点的右子树 (ABCDEFG)
constpreorderTraversal=function(root){constresult=[];functionpushRoot(node){if(node!==null){result.push(node.val);if(node.left!==null){pushRoot(node.left);}if(node.right!==null){pushRoot(node.right);}}}pushRoot(root);returnresult;};
- 时间复杂度: O(n)
- 空间复杂度: O(n)