-
Notifications
You must be signed in to change notification settings - Fork 634
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
leetcode144:二叉树的前序遍历 #37
Comments
|
前序遍历先访问根节点,再访问左节点,最后访问右节点。主要是看非递归版本的实现 递归实现// 递归版本
const preOrderTraverse = (root) => {
let list = []
const preOrder = (node) => {
if(node !== null) {
// 先访问根节点
list.push(node.val)
// 再访问左节点
preOrder(node.left)
// 最后访问右节点
preOrder(node.right)
}
}
preOrder(root)
return list
} 非递归实现// 非递归版本
const preOrderTraverseUnRecur = (root) => {
let list = [];
let stack = [root];
while(stack.length !== 0) {
const curNode = stack.pop()
const left = curNode.left
const right = curNode.right
// 第一步的时候,先访问的是根节点
list.push(curNode.val)
if(right) {
stack.push(right)
}
// 因为pop是取出最后一个元素,所以我们要确保首先拿到的是左节点
if(left) {
stack.push(left)
}
}
} |
递归实现// 前序遍历
var preorderTraversal = (root) => {
let result = []
var preOrderTraverseNode = (node) => {
if(node) {
// 先根节点
result.push(node.val)
// 然后遍历左子树
preOrderTraverseNode(node.left)
// 再遍历右子树
preOrderTraverseNode(node.right)
}
}
preOrderTraverseNode(root)
return result
}; 迭代实现利用栈来记录遍历的过程,实际上,递归就使用了调用栈,所以这里我们可以使用栈来模拟递归的过程
依次循环出栈遍历入栈,直到栈为空,遍历完成 // 前序遍历
const preorderTraversal = (root) => {
const list = [];
const stack = [];
// 当根节点不为空的时候,将根节点入栈
if(root) stack.push(root)
while(stack.length > 0) {
const curNode = stack.pop()
// 第一步的时候,先访问的是根节点
list.push(curNode.val)
// 我们先打印左子树,然后右子树
// 所以先加入栈的是右子树,然后左子树
if(curNode.right !== null) {
stack.push(curNode.right)
}
if(curNode.left !== null) {
stack.push(curNode.left)
}
}
return list
} 复杂度分析: 空间复杂度:O(n) 时间复杂度:O(n) |
var preorderTraversal = function (root) {
if (!root) return [];
let stack = [root];
let res = [];
while (stack.length) {
let node = stack.pop();
res.push(node.val);
if (node.right) {
stack.push(node.right);
}
if (node.left) {
stack.push(node.left);
}
}
return res;
}; |
非递归版 function preorderTraversal(root) {
if (!root) {
return [];
}
const stack = [root];
const result = [];
while (stack.length > 0) {
const node = stack.pop();
result.push(node.val);
// 先将右子树入栈,再将左子树入栈,保证先遍历左子树
if (node.right) {
stack.push(node.right);
}
if (node.left) {
stack.push(node.left);
}
}
return result;
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
给定一个二叉树,返回它的 前序 遍历。
示例:
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
附赠leetcode地址:leetcode
The text was updated successfully, but these errors were encountered: