当前位置: 首页 > news >正文

自己做商城网站小红书软文案例

自己做商城网站,小红书软文案例,图片免费模板,wordpress thems题目链接 文章目录 Python3方法一: 广度优先搜索 (BFS) ⟮ O ( n ) ⟯ \lgroup O(n) \rgroup ⟮O(n)⟯方法二: 深度优先搜索 (DFS) ⟮ O ( n ) ⟯ \lgroup O(n) \rgroup ⟮O(n)⟯ C方法一: 广度优先搜索 (BFS) ⟮ O ( n ) ⟯ \lgroup O(n…

题目链接


在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

Python3

方法一: 广度优先搜索 (BFS) ⟮ O ( n ) ⟯ \lgroup O(n) \rgroup O(n)⟯

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:if not root:return []res = []queue =   [root,] while queue: tmp = [node.val for node in queue]res.append(tmp) # 取当前层 的结点值lis = [] ## 下一层 的结点for node in queue:if node.left:lis.append(node.left)if node.right:lis.append(node.right)queue = lis return res
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:if not root:return []ans = []dq = deque([root])while dq: level = [] ## 当前 遍历层n = len(dq)for _ in range(n):cur = dq.popleft()level.append(cur.val)# 下一层 存到  双端 队列 后面if cur.left:dq.append(cur.left)if cur.right:dq.append(cur.right)ans.append(level)return ans

在这里插入图片描述

方法二: 深度优先搜索 (DFS) ⟮ O ( n ) ⟯ \lgroup O(n) \rgroup O(n)⟯

参考链接

DFS 做本题的主要问题是: DFS 不是按照层次遍历的。为了让递归的过程中同一层的节点放到同一个列表中,在递归时要记录每个节点的深度。递归到新节点要把该节点 对应深度列表的末尾。

在这里插入图片描述

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:# 子模块def helper(node, depth):# 记住结点的 深度if not node: return if len(res) == depth:res.append([])res[depth].append(node.val)if node.left: helper(node.left, depth+1)if node.right: helper(node.right, depth+1)# 主模块if not root: return []res = []helper(root, 0)return res 

C++

方法一: 广度优先搜索 (BFS) ⟮ O ( n ) ⟯ \lgroup O(n) \rgroup O(n)⟯

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode() : val(0), left(nullptr), right(nullptr) {}*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:vector<vector<int>> levelOrder(TreeNode* root) {vector<vector<int>> res;if (root == nullptr){return res;}queue <TreeNode*> q;q.emplace(root);while (!q.empty()){int n = q.size();res.emplace_back(vector<int> ());// 提前添加 空容器for (int i = 0; i < n; ++i){auto node = q.front(); q.pop();res.back().emplace_back(node->val);if (node->left){q.emplace(node->left);}if (node->right){q.emplace(node->right);}}}return res;}
};
http://www.hkea.cn/news/403784/

相关文章:

  • 手机网站代码下载黄页网站推广服务
  • 做网站前端多少钱在线bt种子
  • wordpress+模版+推荐专业网站seo推广
  • 浦项建设公司员工网站2023免费推广入口
  • 如何查询某个网站的设计公司最新推广注册app拿佣金
  • 八宝山做网站公司打广告
  • wordpress vip查看插件南宁seo费用服务
  • 建站之星模板怎么设置手机如何做网站
  • 上海公司网站制作价格西安百度关键词排名服务
  • 长沙网页制作开发公司aso优化方案
  • 深圳罗湖网站制作成人电脑基础培训班
  • 无锡网站制作咨询深圳网站设计十年乐云seo
  • 大连城市建设网站seo优化顾问服务阿亮
  • 福州 网站建设沈阳seo关键词排名优化软件
  • 做网站还要买服务器吗镇江seo
  • 专门做特价的网站优化排名案例
  • 网站建设的一些问题友链交易交易平台
  • 创业初期要建立公司的网站吗seo排名优化代理
  • 做网站全屏尺寸是多少钱站长工具查询系统
  • 做企业平台的网站有哪些手机网站制作教程
  • 免费行情的软件大全下载北京公司排名seo
  • 网站联系方式要素qq群推广链接
  • div css 网站模板免费的云服务器有哪些
  • 35互联做网站好吗网店运营工作内容
  • 网站建设模拟软件营销培训课程内容
  • 深圳建网站兴田德润专业2023年最新新闻简短摘抄
  • 学校网站怎么查询录取百度相册登录入口
  • 自助建设彩票网站网址查询工具
  • 怎么创建网页的快捷方式seo入门版
  • 互联网企业网站网络优化