1.编辑器

我使用的是win10+vscode+leetcode+python3
环境配置参见我的博客:
链接

2.第二百二十六题

(1)题目
英文:
Invert a binary tree.

中文:
翻转一棵二叉树。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/invert-binary-tree

(2)解法
① 递归
(耗时:44ms,内存:13.5M)

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:def invertTree(self, root: TreeNode) -> TreeNode:if not root: return rootroot.left, root.right = self.invertTree(root.right), self.invertTree(root.left)return root

② 迭代
(耗时:36ms,内存:13.5M)

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:def invertTree(self, root: TreeNode) -> TreeNode:cur = deque([root])while cur:node = cur.popleft()if node.left: cur.append(node.left)if node.right: cur.append(node.right)node.left, node.right = node.right, node.leftreturn root

leetcode python3 简单题226. Invert Binary Tree相关推荐

  1. leetcode python3 简单题110. Balanced Binary Tree

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百一十题 (1)题目 英文: Given a binary tree, dete ...

  2. leetcode python3 简单题67. Add Binary

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第六十七题 (1)题目 英文: Given two binary strings, ...

  3. 226. Invert Binary Tree 1

    题目链接:Invert Binary Tree 思路: 如果需要反转一个二叉树,那么我们需要遍历整个树的所有节点. 如果想遍历所有的节点,我们可以用Depth First Search(DFS)或者B ...

  4. leetcode python3 简单题111. Minimum Depth of Binary Tree

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百一十一题 (1)题目 英文: Given a binary tree, fin ...

  5. leetcode python3 简单题108. Convert Sorted Array to Binary Search Tree

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百零八题 (1)题目 英文: Given an array where elem ...

  6. leetcode python3 简单题104. Maximum Depth of Binary Tree

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百零四题 (1)题目 英文: Given a binary tree, find ...

  7. leetcode python3 简单题101. Symmetric Tree

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百零一题 (1)题目 英文: Given a binary tree, chec ...

  8. leetcode python3 简单题100. Same Tree

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百题 (1)题目 英文: Given two binary trees, wri ...

  9. LeetCode #226 - Invert Binary Tree - Easy

    Problem Invert a binary tree. Example 4/ \2 7/ \ / \ 1 3 6 9->4/ \7 2/ \ / \ 9 6 3 1 Algorithm 整理 ...

最新文章

  1. Python 科学计算库 Numpy (二) —— 索引及切片
  2. Android中活动Activity方面的知识点
  3. openvswitch2.8.1 centos7.4 源码编译安装
  4. 实现自己的.NET Core配置Provider之Yaml
  5. 加白名单_Android保活从入门到放弃:乖乖引导用户加白名单吧
  6. 解决设置了display:none的元素,会先展示再隐藏
  7. c语言程序设计 第三版 哈工大,c语言程序设计 哈工大 苏小红 第三章习题
  8. 论信息化投标低于1元中标值吗?
  9. 【DevCloud · 敏捷智库】Scrum和看板如何选择
  10. .net如何获取文件夹中的文件_access递归列出文件夹中的文件
  11. c++如何打印一维数组首地址_4.1 数组的定义
  12. 【SAP技术汇】说说SAP那些事儿
  13. ip地址二进制转十进制
  14. Ns3 构建哑铃型拓扑,并实现两个点的TCP连接(详细请戳全文)
  15. Google 元素定制 T 恤
  16. windows安装JDK步骤
  17. 计算机信息技术知识点思维导图,思维导图信息技术的学习方法
  18. pmp考试中变更的处理流程
  19. 天津大学仁爱学院哪个计算机专业2013山西理科录取分数,天津大学仁爱学院2020年录取分数线(附2017-2020年分数线)...
  20. js 百分比计算公式,图形填充的百分比

热门文章

  1. mysql 设置 server id_详解Mysql存储引擎
  2. 牛客寒假算法基础训练营6
  3. pytorch学习笔记(二):自动求梯度
  4. 【题解】(排序) —— POJ 0811:牛的选举
  5. 牛客 2021年度训练联盟热身训练赛第二场 C题Tip to be Palindrome
  6. 第八届“图灵杯”NEUQ-ACM程序设计竞赛个人赛——B题 小宝的幸运数组
  7. 远程控制——服务器公网IP连接
  8. 【亲测有效】装了双系统后在ubuntu下耳机没有声音的解决方法
  9. 【MySQL】明明加了索引,为何不生效?
  10. java 验证码生成和验证