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

平顶山营销型网站建设清远最新通知

平顶山营销型网站建设,清远最新通知,cc网站域名注册,wordpress更新文件放在哪里目录 一、二叉树 1. 二叉树的深度遍历(DFS:前序、中序、后序遍历) 2. 二叉树的最大深度 3. 翻转二叉树 4. 对称二叉树 5. 二叉树的直径 6. 二叉树的层序遍历 7. 将有序数组转换为二叉搜索树 8. 验证二叉搜索树 9. 二叉搜索树中第 K 小的元素 …

目录

一、二叉树

1. 二叉树的深度遍历(DFS:前序、中序、后序遍历)

2. 二叉树的最大深度

3. 翻转二叉树

4. 对称二叉树

5. 二叉树的直径

6. 二叉树的层序遍历

7. 将有序数组转换为二叉搜索树

8. 验证二叉搜索树

9. 二叉搜索树中第 K 小的元素

10. 二叉树的右视图

(待更...)

二、图论(待更...)


前言

一、二叉树:二叉树的中序遍历,二叉树的最大深度,翻转二叉树,对称二叉树,二叉树的直径,二叉树的层序遍历,将有序数组转换为二叉搜索树,验证二叉搜索树,二叉搜索树中第 K 小的元素,二叉树的右视图...... (日更ing)

二、图论:...... (日更ing)


一、二叉树

1. 二叉树的深度遍历(DFS:前序、中序、后序遍历)

 原题链接:94. 二叉树的中序遍历 - 力扣(LeetCode)

# (1)前序遍历:根-左-右
class Solution(object):def preorderTraversal(self, root):res = []def preorder(root):if not root:return res.append(root.val)preorder(root.left)preorder(root.right)preorder(root)return res
# (2)中序遍历:左-根-右
class Solution(object):def inorderTraversal(self, root):res = []def inorder(root):if not root:return inorder(root.left)res.append(root.val)inorder(root.right)inorder(root)return res

# (3)后序遍历:左-右-根
class Solution(object):def postorderTraversal(self, root):res = []def inorder(root):if not root:return postorder(root.left)postorder(root.right)res.append(root.val)postorder(root)return res

2. 二叉树的最大深度

原题链接:104. 二叉树的最大深度 - 力扣(LeetCode)

class Solution(object):def maxDepth(self, root):if not root:return 0left_height = self.maxDepth(root.left)right_height = self.maxDepth(root.right)return max(left_height, right_height) + 1

3. 翻转二叉树

原题链接:226. 翻转二叉树 - 力扣(LeetCode)

class Solution(object):def invertTree(self, root):if not root:return root.left, root.right = root.right, root.leftself.invertTree(root.left)self.invertTree(root.right)return root

4. 对称二叉树

原题链接:101. 对称二叉树 - 力扣(LeetCode)

class Solution(object):def isSymmetric(self, root):def check(left, right):if not left and not right:return Trueif not left or not right:return Falseif left.val != right.val:return Falsereturn check(left.left, right.right) and check(left.right, right.left)return check(root.left, root.right)

5. 二叉树的直径

原题链接:543. 二叉树的直径 - 力扣(LeetCode)

class Solution(object):def diameterOfBinaryTree(self, root):def dfs(root):if not root:return 0, 0ld, ldepth = dfs(root.left)rd, rdepth = dfs(root.right)return max(ld, rd, ldepth+rdepth), max(ldepth, rdepth) + 1return dfs(root)[0]

6. 二叉树的层序遍历

原题链接:102. 二叉树的层序遍历 - 力扣(LeetCode)

class Solution(object):def levelOrder(self, root):if not root:return []node = [root]res = []while len(node) > 0:res.append([i.val for i in node])node2 = []for i in node:if i.left:node2.append(i.left)if i.right:node2.append(i.right)node = node2return res

7. 将有序数组转换为二叉搜索树

原题链接:108. 将有序数组转换为二叉搜索树 - 力扣(LeetCode)

class Solution(object):def sortedArrayToBST(self, nums):def dfs(left, right):if left > right:returnmid = (left + right) // 2root = TreeNode(nums[mid])root.left = dfs(left, mid-1)root.right = dfs(mid+1, right)return rootreturn dfs(0, len(nums)-1)

8. 验证二叉搜索树

原题链接:98. 验证二叉搜索树 - 力扣(LeetCode)

class Solution(object):def isValidBST(self, root, left=float('-inf'), right=float('inf')):if not root:return Truex = root.valreturn left < x < right and self.isValidBST(root.left, left, x) and self.isValidBST(root.right, x, right)
# self.isValidBST(root.left, left, x):遍历左子树,右边界更新
# self.isValidBST(root.right, x, right):遍历右子树,左边界更新

9. 二叉搜索树中第 K 小的元素

原题链接:230. 二叉搜索树中第 K 小的元素 - 力扣(LeetCode)

# 先通过前序/中序/后序遍历转为list,而后利用list属性找第k个小的元素。
# 此代码使用前序遍历(根-左-右)
class Solution(object):def kthSmallest(self, root, k):res = [] def preorder(root):if not root:returnres.append(root.val)preorder(root.left)preorder(root.right)return resres = preorder(root)res.sort()return res[k-1]

10. 二叉树的右视图

原题链接:199. 二叉树的右视图 - 力扣(LeetCode)

class Solution(object):def rightSideView(self, root):# if len(res) == depth: res.append(root.val)# 先遍历右子树,再遍历左子树res = []def dfs(root, depth):if not root:return []if len(res) == depth:res.append(root.val)dfs(root.right, depth+1)dfs(root.left, depth+1)return resreturn dfs(root, 0)


二、图论

http://www.dtcms.com/a/530073.html

相关文章:

  • 江苏网站推广商标设计网站推荐
  • 建设企业网站需注意什么中国建设劳动学会网站
  • 服务器网站打开慢建设银行人才招聘官方网站
  • 网页设计制作网站html代码杭州网络科技公司排名
  • 上传到网站的根目录中四川成都旅游必去十大景点推荐
  • 宝塔建设网站上海的网站开发公司电话
  • 怎么看网站是否织梦门户网站建设滞后
  • 深喉咙企业网站官方网站建设需求规格说明书
  • 做相亲网站犯法吗vs2015做网站的后端
  • 新源网站建设手机网站模板源码
  • Linux中文件路径解析函数path_walk的实现
  • 群辉wordpress东莞市网络优化推广服务机构
  • 网站建设的安全威胁wordpress customize.php
  • 学做快餐的视频网站传奇网页版游戏开服表
  • 镇江网站搜索排名云主机网站如何备份
  • 学校网站设计及代码国内最好的摄影网站
  • 公文写作网站公司宣传册设计模板
  • 民权做网站哪家好wordpress ip_hash失效
  • 计算机网络自顶向下方法2——网络、ISP连接结构介绍
  • 速卖通网站怎样做店面的二维码百度网站广告怎么做
  • 【笔记】修复 ComfyUI 启动报错 KeyError: ‘luts‘ 和 KeyError: ‘tensorrt‘
  • 建设网站应注意什么网站制作公司美股上市
  • 网站源码怎么下载网站建设课程设计报告php
  • 为什么电脑打开那个做网站都是那一个信息系统管理工程师
  • 【右值引用完美转发】右值引用与完美转发的“天罡北斗阵”
  • 链表-循环双向链表【node4】
  • 襄阳网站建设找下拉哥科技sem seo是什么意思呢
  • 移动端网站建设的软件有哪些建设银行投诉网站首页
  • 乌鲁木做兼职的网站三合一网站建设方案
  • 西安装修公司网站制作做一个网站赚钱吗