php网站模版,网站推广工作如何做,苏州网络推广推广,系统网站自助建站链表
反转单向链表
该题⽬来⾃ LeetCode#xff0c;题⽬需要将⼀个单向链表反转。思路很简单#xff0c;使⽤三个变量分别表示当前节点和当前节点的前后节点#xff0c;虽然这题很简单#xff0c;但是却是⼀道⾯试常考题
var reverseList function(head) {
// 判断下变…链表
反转单向链表
该题⽬来⾃ LeetCode题⽬需要将⼀个单向链表反转。思路很简单使⽤三个变量分别表示当前节点和当前节点的前后节点虽然这题很简单但是却是⼀道⾯试常考题
var reverseList function(head) {
// 判断下变量边界问题
if (!head || !head.next) return head
// 初始设置为空因为第⼀个节点反转后就是尾部尾部节点指向 null
let pre null
let current head
let next
// 判断当前节点是否为空
// 不为空就先获取当前节点的下⼀节点
// 然后把当前节点的 next 设为上⼀个节点
// 然后把 current 设为下⼀个节点pre 设为当前节点
while(current) {
next current.next
current.next pre树
⼆叉树的先序中序后序遍历
先序遍历表示先访问根节点然后访问左节点最后访问右节点。中序遍历表示先访问左节点然后访问根节点最后访问右节点。后序遍历表示先访问左节点然后访问右节点最后访问根节点
递归实现
递归实现相当简单代码如下
pre current
current next
}
return pre
};
function TreeNode(val) {
this.val val;
this.left this.right null;
}
var traversal function(root) {
if (root) {
// 先序
console.log(root);
traversal(root.left);
// 中序
// console.log(root);
traversal(root.right);
// 后序
// console.log(root);
}
};对于递归的实现来说只需要理解每个节点都会被访问三次就明⽩为什么这样实现了
⾮递归实现
⾮递归实现使⽤了栈的结构通过栈的先进后出模拟递归实现。
以下是先序遍历代码实现
function pre(root) {
if (root) {
let stack [];
// 先将根节点 push
stack.push(root);
// 判断栈中是否为空
while (stack.length 0) {
// 弹出栈顶元素
root stack.pop();
console.log(root);
// 因为先序遍历是先左后右栈是先进后出结构
// 所以先 push 右边再 push 左边
if (root.right) {
stack.push(root.right);
}
if (root.left) {
stack.push(root.left);
}
}
}
}以下是中序遍历代码实现
function mid(root) {
if (root) {
let stack [];
// 中序遍历是先左再根最后右
// 所以⾸先应该先把最左边节点遍历到底依次 push 进栈
// 当左边没有节点时就打印栈顶元素然后寻找右节点
// 对于最左边的叶节点来说可以把它看成是两个 null 节点的⽗节点
// 左边打印不出东⻄就把⽗节点拿出来打印然后再看右节点
while (stack.length 0 || root) {
if (root) {
stack.push(root);
root root.left;
} else {
root stack.pop();
console.log(root);
root root.right;
}
}以下是后序遍历代码实现该代码使⽤了两个栈来实现遍历相⽐⼀个栈的遍历来说要容易理解很多
function pos(root) {
if (root) {
let stack1 [];
let stack2 [];
// 后序遍历是先左再右最后根
// 所以对于⼀个栈来说应该先 push 根节点
// 然后 push 右节点最后 push 左节点
stack1.push(root);
while (stack1.length 0) {
root stack1.pop();
stack2.push(root);
if (root.left) {
stack1.push(root.left);
}
if (root.right) {
stack1.push(root.right);
}
}
while (stack2.length 0) {
console.log(s2.pop());
}
}
}中序遍历的前驱后继节点
实现这个算法的前提是节点有⼀个 parent 的指针指向⽗节点根节点指向null该树的中序遍历结果是 4, 2, 5, 1, 6, 3, 7
前驱节点
对于节点 2 来说他的前驱节点就是 4 按照中序遍历原则可以得出以下结论如果选取的节点的左节点不为空就找该左节点最右的节点。对于节点 1 来说他有左节点 2 那么节点 2 的最右节点就是 5如果左节点为空且⽬标节点是⽗节点的右节点那么前驱节点为⽗节点。对于节点 5 来说没有左节点且是节点 2 的右节点所以节点 2 是前驱节点如果左节点为空且⽬标节点是⽗节点的左节点向上寻找到第⼀个是⽗节点的右节点的节点。对于节点 6 来说没有左节点且是节点 3 的左节点所以向上寻找到节点 1 发现节点 3 是节点 1 的右节点所以节点 1 是节点 6 的前驱节点
以下是算法实现
function predecessor(node) {
if (!node) return
// 结论 1
if (node.left) {
return getRight(node.left)
} else {
let parent node.parent
// 结论 2 3 的判断
while(parent parent.right node) {
node parent
parent node.parent
}
return parent
}
}
function getRight(node) {
if (!node) return
node node.right
while(node) node node.right
return node
}后继节点
对于节点 2 来说他的后继节点就是 5 按照中序遍历原则可以得出以下结论如果有右节点就找到该右节点的最左节点。对于节点 1 来说他有右节点 3 那么节点3 的最左节点就是 6如果没有右节点就向上遍历直到找到⼀个节点是⽗节点的左节点。对于节点 5 来说没有右节点就向上寻找到节点 2 该节点是⽗节点 1 的左节点所以节点 1 是后继节点 以 下是算法实现
function successor(node) {
if (!node) return
// 结论 1
if (node.right) {
return getLeft(node.right)
} else {
// 结论 2
let parent node.parent
// 判断 parent 为空
while(parent parent.left node) {
node parent
parent node.parent
}
return parent
}
}
function getLeft(node) {
if (!node) return
node node.left
while(node) node node.left
return node
}树的深度
树的最⼤深度该题⽬来⾃ Leetcode题⽬需要求出⼀颗⼆叉树的最⼤深度以下是算法实现
var maxDepth function(root) {
if (!root) return 0
return Math.max(maxDepth(root.left), maxDepth(root.right)) 1
};对于该递归函数可以这样理解⼀旦没有找到节点就会返回 0每弹出⼀次递归函数就会加⼀树有三层就会得到3