|
1 | | -# React + TypeScript + Vite |
2 | | - |
3 | | -This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules. |
4 | | - |
5 | | -Currently, two official plugins are available: |
6 | | - |
7 | | -- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react) uses [Babel](https://babeljs.io/) for Fast Refresh |
8 | | -- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh |
9 | | - |
10 | | -## Expanding the ESLint configuration |
11 | | - |
12 | | -If you are developing a production application, we recommend updating the configuration to enable type-aware lint rules: |
13 | | - |
14 | | -```js |
15 | | -export default tseslint.config({ |
16 | | - extends: [ |
17 | | - // Remove ...tseslint.configs.recommended and replace with this |
18 | | - ...tseslint.configs.recommendedTypeChecked, |
19 | | - // Alternatively, use this for stricter rules |
20 | | - ...tseslint.configs.strictTypeChecked, |
21 | | - // Optionally, add this for stylistic rules |
22 | | - ...tseslint.configs.stylisticTypeChecked, |
23 | | - ], |
24 | | - languageOptions: { |
25 | | - // other options... |
26 | | - parserOptions: { |
27 | | - project: ['./tsconfig.node.json', './tsconfig.app.json'], |
28 | | - tsconfigRootDir: import.meta.dirname, |
29 | | - }, |
30 | | - }, |
31 | | -}) |
| 1 | +# 翻转二叉树算法动画演示 |
| 2 | + |
| 3 | +这个项目是 LeetCode 226 翻转二叉树问题的交互式可视化实现。通过动画效果直观展示了不同解法的执行过程。 |
| 4 | + |
| 5 | +## 项目特点 |
| 6 | + |
| 7 | +- 支持三种翻转二叉树的算法: |
| 8 | + - 递归解法 |
| 9 | + - 迭代解法 (队列实现) |
| 10 | + - 迭代解法 (栈实现) |
| 11 | +- 动态展示算法执行过程中的每一步 |
| 12 | +- 交互式控制面板,可暂停、播放、单步执行 |
| 13 | +- 节点高亮与动画效果直观展示算法执行细节 |
| 14 | +- 完全模块化的代码结构,易于理解和扩展 |
| 15 | + |
| 16 | +## 技术栈 |
| 17 | + |
| 18 | +- React 19 |
| 19 | +- TypeScript |
| 20 | +- D3.js 用于树的可视化 |
| 21 | +- CSS3 动画效果 |
| 22 | + |
| 23 | +## 如何运行 |
| 24 | + |
| 25 | +克隆项目后,执行以下命令: |
| 26 | + |
| 27 | +```bash |
| 28 | +# 安装依赖 |
| 29 | +npm install |
| 30 | + |
| 31 | +# 开发模式运行 |
| 32 | +npm run dev |
| 33 | + |
| 34 | +# 构建项目 |
| 35 | +npm run build |
32 | 36 | ``` |
33 | 37 |
|
34 | | -You can also install [eslint-plugin-react-x](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-x) and [eslint-plugin-react-dom](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-dom) for React-specific lint rules: |
35 | | - |
36 | | -```js |
37 | | -// eslint.config.js |
38 | | -import reactX from 'eslint-plugin-react-x' |
39 | | -import reactDom from 'eslint-plugin-react-dom' |
40 | | - |
41 | | -export default tseslint.config({ |
42 | | - plugins: { |
43 | | - // Add the react-x and react-dom plugins |
44 | | - 'react-x': reactX, |
45 | | - 'react-dom': reactDom, |
46 | | - }, |
47 | | - rules: { |
48 | | - // other rules... |
49 | | - // Enable its recommended typescript rules |
50 | | - ...reactX.configs['recommended-typescript'].rules, |
51 | | - ...reactDom.configs.recommended.rules, |
52 | | - }, |
53 | | -}) |
| 38 | +## 算法说明 |
| 39 | + |
| 40 | +### 递归解法 |
| 41 | + |
| 42 | +递归解法是最简洁的实现方式,通过递归交换每个节点的左右子树实现翻转。 |
| 43 | + |
| 44 | +```typescript |
| 45 | +function invertTree(root) { |
| 46 | + if (!root) return null; |
| 47 | + |
| 48 | + const left = invertTree(root.left); |
| 49 | + const right = invertTree(root.right); |
| 50 | + |
| 51 | + root.left = right; |
| 52 | + root.right = left; |
| 53 | + |
| 54 | + return root; |
| 55 | +} |
54 | 56 | ``` |
| 57 | + |
| 58 | +### 迭代解法 (队列) |
| 59 | + |
| 60 | +使用队列实现广度优先遍历,并在遍历过程中交换每个节点的左右子树。 |
| 61 | + |
| 62 | +### 迭代解法 (栈) |
| 63 | + |
| 64 | +使用栈实现深度优先遍历,并在遍历过程中交换每个节点的左右子树。 |
| 65 | + |
| 66 | +## 项目结构 |
| 67 | + |
| 68 | +``` |
| 69 | +src/ |
| 70 | +├── algorithms/ # 算法实现代码 |
| 71 | +├── components/ # React 组件 |
| 72 | +├── models/ # 数据模型定义 |
| 73 | +├── utils/ # 工具函数 |
| 74 | +└── App.tsx # 主应用 |
| 75 | +``` |
| 76 | + |
| 77 | +## 许可证 |
| 78 | + |
| 79 | +MIT |
0 commit comments