Skip to content

Commit

Permalink
算法:递归
Browse files Browse the repository at this point in the history
  • Loading branch information
GGXXMM committed Jul 26, 2023
1 parent 285c55c commit 54dd429
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 3 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

### 算法
1. [排序算法]()
2. [递归]()
2. [递归](./常考算法题/递归)
3. [回溯算法]()
4. [深度优先和广度优先搜索](./常考算法题/DFS+BFS)
5. [二分查找](./常考算法题/二分查找)
Expand Down Expand Up @@ -190,8 +190,12 @@
3. [webpack的热更新使用及原理分析(是如何做到在不刷新浏览器的前提下更新页面的)](https://github.com/GGXXMM/FE-Knowledge/issues/85)
4. [webpack的性能优化](https://github.com/GGXXMM/FE-Knowledge/issues/86)
5. [webpack构建原理](https://github.com/GGXXMM/FE-Knowledge/issues/87)
6. [Rollup基础配置?及其构建原理?](https://github.com/GGXXMM/fe-knowledge/issues/151)
7. [vite为什么比webpack快?vite工作原理?](https://github.com/GGXXMM/FE-Knowledge/issues/132)

### Rollup
1. [Rollup基础配置?及其构建原理?](https://github.com/GGXXMM/fe-knowledge/issues/151)

### Vite
1. [vite为什么比webpack快?vite工作原理?](https://github.com/GGXXMM/FE-Knowledge/issues/132)

### babel
1. [Babel的原理是什么?](https://github.com/GGXXMM/FE-Knowledge/issues/95)
Expand Down
31 changes: 31 additions & 0 deletions 常考算法题/递归/144.二叉树的前序遍历.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* @lc app=leetcode.cn id=144 lang=javascript
*
* [144] 二叉树的前序遍历
*/

// @lc code=start
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
// 递归
var preorderTraversal = function(root, arr = []) {
if(root) {
// 根 -> 左 -> 右
arr.push(root.val)
preorderTraversal(root.left, arr)
preorderTraversal(root.right, arr)
}
return arr
};
// @lc code=end

31 changes: 31 additions & 0 deletions 常考算法题/递归/145.二叉树的后序遍历.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* @lc app=leetcode.cn id=145 lang=javascript
*
* [145] 二叉树的后序遍历
*/

// @lc code=start
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
// 递归
var postorderTraversal = function(root, arr = []) {
if(root) {
// 左 -> 右 -> 根
postorderTraversal(root.left, arr)
postorderTraversal(root.right, arr)
arr.push(root.val)
}
return arr
};
// @lc code=end

40 changes: 40 additions & 0 deletions 常考算法题/递归/50.pow-x-n.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* @lc app=leetcode.cn id=50 lang=javascript
*
* [50] Pow(x, n)
*/
// 题目:https://leetcode.cn/problems/powx-n/
// @lc code=start
/**
* @param {number} x
* @param {number} n
* @return {number}
*/
// 1、调用库函数:时间复杂度:O(1)
var myPow = function(x, n) {
return Math.pow(x, n);
}
// 2、暴力法:时间复杂度:O(n)
var myPow = function(x, n) {
if(n < 0) {
x = 1/x;
n = -n;
}
let result = 1;
for(let i = 0;i < n;i++) {
result = result * x;
}
return result;
}
// 3、递归+分治:时间复杂度:O(logn)
var myPow = function(x, n) {
if(n === 0) return 1;// n为0直接返回1
if(n < 0) {// n是负数
return 1/myPow(x,-n);
}
if(n%2) {//n是奇数
return x*myPow(x,n-1);
}
return myPow(x*x,n/2) //n是偶数,使用分治,一分为二,等于x*x的n/2次方
};
// @lc code=end
31 changes: 31 additions & 0 deletions 常考算法题/递归/94.二叉树的中序遍历.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* @lc app=leetcode.cn id=94 lang=javascript
*
* [94] 二叉树的中序遍历
*/

// @lc code=start
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
// 递归
var inorderTraversal = function(root, arr = []) {
if(root) {
// 左 -> 根 -> 右
inorderTraversal(root.left, arr)
arr.push(root.val)
inorderTraversal(root.right, arr)
}
return arr
};
// @lc code=end

0 comments on commit 54dd429

Please sign in to comment.