![](https://img-blog.csdnimg.cn/20210111204015388.jpg?x-oss-process=image/resize,m_fixed,h_224,w_224)
链表/树算法
链表/树算法
IamSmaLLR.
这个作者很懒,什么都没留下…
展开
-
二叉搜索树的第K个结点(剑指offer62)
/* function TreeNode(x) { this.val = x; this.left = null; this.right = null;} */function KthNode(pRoot, k){ let stack = [] let result = [] let current = pRoot while(current || stack.length){ if(current){ .原创 2021-03-04 17:15:57 · 110 阅读 · 1 评论 -
合并二叉树
/* * function TreeNode(x) { * this.val = x; * this.left = null; * this.right = null; * } *//** * * @param t1 TreeNode类 * @param t2 TreeNode类 * @return TreeNode类 */function mergeTrees( t1 , t2 ) { // write code here if(!t1&a.原创 2021-02-26 02:08:31 · 67 阅读 · 0 评论 -
二叉树的右视图
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * 求二叉树的右视图 * @param xianxu int整型一维数组 先序遍历 * @param zhongxu int整型一维数组 中序遍历 * @return int整型一维数组 */function solve( xianxu , zhongxu ) { // write code here function TreeNode(x){ this.val = x .原创 2021-02-21 17:03:25 · 86 阅读 · 0 评论 -
判断是否是平衡二叉树
/* function TreeNode(x) { this.val = x; this.left = null; this.right = null;} */function IsBalanced_Solution(pRoot){ // write code here if(!pRoot) return true function dfs(root){ if(!root) return 0 return Math.ma.原创 2021-02-18 16:20:45 · 81 阅读 · 0 评论 -
单链表的归并排序
/* * function ListNode(x){ * this.val = x; * this.next = null; * } *//** * * @param head ListNode类 the head node * @return ListNode类 */function sortInList( head ) { if(head==null||head.next==null){ return head } let slo原创 2021-02-18 16:04:43 · 65 阅读 · 0 评论 -
在二叉树中找到两个结点的最近公共祖先节点
/* * function TreeNode(x) { * this.val = x; * this.left = null; * this.right = null; * } *//** * * @param root TreeNode类 * @param o1 int整型 * @param o2 int整型 * @return int整型 */function lowestCommonAncestor( root , o1 , o2 ) { .原创 2021-02-17 21:30:46 · 222 阅读 · 0 评论 -
两个链表的第一个公共结点
/*function ListNode(x){ this.val = x; this.next = null;}*/function FindFirstCommonNode(pHead1, pHead2){ // write code here var p1 = pHead1 var p2 = pHead2 while(p1!==p2){ p1 = p1 == null ? pHead2 : p1.next p2 = p.原创 2021-02-15 22:56:28 · 73 阅读 · 0 评论 -
判断链表是否为回文结构
/* * function ListNode(x){ * this.val = x; * this.next = null; * } *//** * * @param head ListNode类 the head * @return bool布尔型 */function isPail( head ) { if(!head) return true; var stack = []; var p = head; var s = head; .原创 2021-02-15 16:51:00 · 88 阅读 · 0 评论 -
建立镜像二叉树
/* function TreeNode(x) { this.val = x; this.left = null; this.right = null;} */function Mirror(root){ // write code here if(!root) return null function buildImage(root){ if(!root.left && !root.right) return .原创 2021-02-15 13:09:57 · 98 阅读 · 0 评论 -
判断二叉树是否对称
/* * function TreeNode(x) { * this.val = x; * this.left = null; * this.right = null; * } *//** * * @param root TreeNode类 * @return bool布尔型 */function isSymmetric( root ) { // write code here if(!root) return true funct.原创 2021-02-14 16:59:04 · 70 阅读 · 0 评论 -
根据前序和中序遍历重建二叉树
/* function TreeNode(x) { this.val = x; this.left = null; this.right = null;} */function reConstructBinaryTree(pre, vin){ // write code here if(pre.length == 0 || vin.length ==0) return null var root = new TreeNode(pre[0]) v.原创 2021-02-14 16:22:11 · 74 阅读 · 0 评论 -
链表之和
给定两个代表非负数的链表,数字在链表中是反向存储的(链表头结点处的数字是个位数,第二个结点上的数字是十位数...),求这个两个数的和,结果也用链表表示。输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)输出: 7 -> 0 -> 8 /* * function ListNode(x){ * this.val = x; * this....原创 2020-10-04 14:44:35 · 278 阅读 · 0 评论 -
交换链表节点(每两个节点交换一次位置)
将给定的链表中每两个相邻的节点交换一次,返回链表的头指针例如,给出1->2->3->4,你应该返回链表2->1->4->3。你给出的算法只能使用常量级的空间。你不能修改列表中的值,只能修改节点本身。 /* * function ListNode(x){ * this.val = x; * this.next = null; * } *//...原创 2020-10-05 05:23:55 · 1277 阅读 · 1 评论 -
删除链表中重复元素(将重复的节点删除只剩一个)
题目描述删除给出链表中的重复元素(链表中元素从小到大有序),使链表中的所有元素都只出现一次例如:给出的链表为1→1→2,返回1→2.给出的链表为1→1→2→3→3,返回1→2→3. /* * function ListNode(x){ * this.val = x; * this.next = null; * } *//** * * @param head Lis...原创 2020-10-03 21:23:29 · 550 阅读 · 0 评论 -
删除链表重复元素(将重复的节点全部删除)
给出一个升序排序的链表,删除链表中的所有重复出现的元素,只保留原链表中只出现一次的元素。例如:给出的链表为1→2→3→3→4→4→5, 返回1→2→5.给出的链表为1→1→1→2→3, 返回2→3. /* * function ListNode(x){ * this.val = x; * this.next = null; * } *//** * * @param...原创 2020-10-03 17:35:46 · 1164 阅读 · 1 评论 -
删除链表的倒数第n个节点(双指针)
给定一个链表,删除链表的倒数第n个节点并返回链表的头指针例如, 给出的链表为:1->2->3->4->5, n= 2. 删除了链表的倒数第n个节点之后,链表变为1->2->3->5.备注:题目保证n一定是有效的请给出请给出时间复杂度为\ O(n) O(n)的算法 /* * function ListNode(x){ * this.val = x;...原创 2020-10-04 22:06:09 · 68 阅读 · 1 评论 -
合并两个有序链表(创建新链表往里面添加节点)
将两个有序的链表合并为一个新链表,要求新的链表是通过拼接两个链表的节点来生成的。 /* * function ListNode(x){ * this.val = x; * this.next = null; * } *//** * * @param l1 ListNode类 * @param l2 ListNode类 * @return ListNod...原创 2020-10-03 03:01:48 · 99 阅读 · 0 评论 -
逆序链表(反转链表)
/*function ListNode(x){ this.val = x; this.next = null;}*/function ReverseList(pHead){ // write code here var head = {} head.next = pHead if(pHead == null || pHead.next == ...原创 2020-10-03 07:04:30 · 100 阅读 · 0 评论 -
合并k个已排序链表
合并k个已排序的链表并将其作为一个已排序的链表返回。分析并描述其复杂度。 /* * function ListNode(x){ * this.val = x; * this.next = null; * } *//** * * @param lists ListNode类一维数组 * @return ListNode类 */function mergeKLis...原创 2020-10-05 00:46:53 · 95 阅读 · 0 评论 -
二叉树的最大路径和
给定一个二叉树,请计算节点值之和最大的路径的节点值之和是多少。这个路径的开始节点和结束节点可以是二叉树中的任意节点 /* * function TreeNode(x) { * this.val = x; * this.left = null; * this.right = null; * } *//** * * @param root TreeNode类 ...原创 2020-10-14 14:25:30 · 99 阅读 · 0 评论 -
转动链表
将给定的链表向右转动k个位置,k是非负数。例如:给定1->2->3->4->5->null , k=2,返回4->5->1->2->3->null。 /* * function ListNode(x){ * this.val = x; * this.next = null; * } *//** * * @...原创 2020-10-08 01:08:33 · 91 阅读 · 0 评论 -
二叉树的后序遍历
递归解法 var result=[]function postorderTraversal( root ) { // write code here if(root==null){return []} postorderTraversal(root.left) postorderTraversal(root.right) result.push(root...原创 2020-10-14 14:37:25 · 45 阅读 · 0 评论 -
链表中环的入口节点
对于一个给定的链表,返回环的入口节点,如果没有环,返回null拓展:你能给出不利用额外空间的解法么? /* * function ListNode(x){ * this.val = x; * this.next = null; * } *//** * * @param head ListNode类 * @return ListNode类 */function ...原创 2020-10-14 11:01:51 · 59 阅读 · 0 评论 -
二叉树的中序遍历
递归解法 var result = []function inorderTraversal( root ) { // write code here if(!root) {return []} inorderTraversal(root.left) result.push(root.val) inorderTraversal(root.right) ...原创 2020-10-14 18:46:45 · 62 阅读 · 0 评论 -
链表排序
在O(n log n)的时间内使用常数级空间复杂度对链表进行排序。{30,20,40} => {20,30,40} /* * function ListNode(x){ * this.val = x; * this.next = null; * } *//** * * @param head ListNode类 * @return ListNode类...原创 2020-10-13 08:02:19 · 45 阅读 · 0 评论 -
不同的二叉搜索树
/** * * @param n int整型 * @return int整型 *///f(n)+=f(k-1)*f(n-k) k=1,2...nfunction numTrees( n ) { // write code here if(n==0) return 1; var arr =[] arr[0] = 1 arr[1] ...原创 2020-10-14 21:14:39 · 33 阅读 · 0 评论 -
二叉树层序遍历
给定一个二叉树,返回该二叉树层序遍历的结果,(从左到右,一层一层地遍历)例如:给定的二叉树是{3,9,20,#,#,15,7},结果是[[3],[9,20],[15,7]] /* * function TreeNode(x) { * this.val = x; * this.left = null; * this.right = null; * } *//** *...原创 2020-10-14 17:53:10 · 240 阅读 · 0 评论 -
链表内指定区间反转
将一个链表m位置到n位置之间的区间反转,要求时间复杂度 ,空间复杂度 。例如:给出的链表为1→2→3→4→5→NULL,返回1→4→3→2→5→NULL.注意:给出的m,n满足以下条件:链表长度1≤m≤n≤链表长度 /* * function ListNode(x){ * this.val = x; * this.next = null; * } *//** * ...原创 2020-10-14 19:36:51 · 907 阅读 · 0 评论 -
二叉树的前序遍历
递归解法 var result = []function preorderTraversal( root ) { // write code here if(!root){return []} result.push(root.val) preorderTraversal(root.left) preorderTraversal(root.right)...原创 2020-10-14 05:06:30 · 43 阅读 · 0 评论 -
二叉树最小深度/最大深度
求给定二叉树的最小深度。最小深度是指树的根结点到最近叶子结点的最短路径上结点的数量。 /* * function TreeNode(x) { * this.val = x; * this.left = null; * this.right = null; * } *//** * * @param root TreeNode类 * @return int...原创 2020-10-14 23:02:14 · 118 阅读 · 0 评论 -
划分链表
给出一个链表和一个值x ,以x为参照将链表划分成两部分,使所有小于x的节点都位于大于或等于x的节点之前。两个部分之内的节点之间要保持的原始相对顺序。例如:给出1→4→3→2→5→2 和 x=3,返回1→2→2→4→3→5. /* * function ListNode(x){ * this.val = x; * this.next = null; * } *//** *...原创 2020-10-08 04:32:33 · 293 阅读 · 0 评论 -
二叉树根节点到叶子节点的所有路径和
给定一个仅包含数字0−9 的二叉树,每一条从根节点到叶子节点的路径都可以用一个数字表示。例如根节点到叶子节点的一条路径是1→2→3,那么这条路径就用123来代替。找出根节点到叶子节点的所有路径表示的数字之和 /* * function TreeNode(x) { * this.val = x; * this.left = null; * this.right = null;...原创 2020-10-14 21:41:13 · 862 阅读 · 0 评论 -
判断链表中是否存在环
判断给定的链表中是否有环扩展:你能给出空间复杂度O(1)的解法么? /* * function ListNode(x){ * this.val = x; * this.next = null; * } *//** * * @param head ListNode类 * @return bool布尔型 */function hasCycle( head ) {...原创 2020-10-08 02:31:19 · 55 阅读 · 0 评论 -
重排链表
将给定的单链表L: L_0→L_1→…→L_{n-1}→L_n重新排序为:L_0→L_n →L_1→L_{n-1}→L_2→L_{n-2}→…...要求使用原地算法,不能改变节点内部的值,需要对实际的节点进行交换。例如:对于给定的单链表{10,20,30,40},将其重新排序为{10,40,20,30}. /* * function ListNode(x){ * this.val = x...原创 2020-10-13 02:27:30 · 145 阅读 · 0 评论 -
链表中的节点每k个一组翻转
将给出的链表中的节点每 k 个一组翻转,返回翻转后的链表如果链表中的节点数不是 k 的倍数,将最后剩下的节点保持原样你不能更改节点中的值,只能更改节点本身。要求空间复杂度O(1)例如:给定的链表是1→2→3→4→5对于k=2, 你应该返回 2→1→4→3→5对于k=3, 你应该返回 3→2→1→4→5 /* * function ListNode(x){ * this.val = x;...原创 2020-10-08 19:53:51 · 691 阅读 · 0 评论 -
二叉树根节点到叶子节点的和为指定值的路径
/* * function TreeNode(x) { * this.val = x; * this.left = null; * this.right = null; * } *//** * * @param root TreeNode类 * @param sum int整型 * @return int整型二维数组 */functio...原创 2020-10-14 02:15:44 · 354 阅读 · 0 评论 -
判断二叉树是否相等
/* * function TreeNode(x) { * this.val = x; * this.left = null; * this.right = null; * } *//** * * @param p TreeNode类 * @param q TreeNode类 * @return bool布尔型 */function isS...原创 2020-10-14 12:33:10 · 80 阅读 · 0 评论 -
(排序)链表的插入排序
/* * function ListNode(x){ * this.val = x; * this.next = null; * } *//** * * @param head ListNode类 * @return ListNode类 */function insertionSortList( head ) { // write code h...原创 2020-10-20 11:32:16 · 49 阅读 · 0 评论 -
判断二叉树是否为平衡二叉树
/* * function TreeNode(x) { * this.val = x; * this.left = null; * this.right = null; * } *//** * * @param root TreeNode类 * @return bool布尔型 */function isBalanced( root ) { ...原创 2021-01-04 11:36:29 · 53 阅读 · 0 评论