leetcode python3 简单题226. Invert Binary Tree
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相关推荐
- leetcode python3 简单题110. Balanced Binary Tree
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百一十题 (1)题目 英文: Given a binary tree, dete ...
- leetcode python3 简单题67. Add Binary
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第六十七题 (1)题目 英文: Given two binary strings, ...
- 226. Invert Binary Tree 1
题目链接:Invert Binary Tree 思路: 如果需要反转一个二叉树,那么我们需要遍历整个树的所有节点. 如果想遍历所有的节点,我们可以用Depth First Search(DFS)或者B ...
- leetcode python3 简单题111. Minimum Depth of Binary Tree
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百一十一题 (1)题目 英文: Given a binary tree, fin ...
- leetcode python3 简单题108. Convert Sorted Array to Binary Search Tree
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百零八题 (1)题目 英文: Given an array where elem ...
- leetcode python3 简单题104. Maximum Depth of Binary Tree
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百零四题 (1)题目 英文: Given a binary tree, find ...
- leetcode python3 简单题101. Symmetric Tree
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百零一题 (1)题目 英文: Given a binary tree, chec ...
- leetcode python3 简单题100. Same Tree
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百题 (1)题目 英文: Given two binary trees, wri ...
- 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 整理 ...
最新文章
- Python 科学计算库 Numpy (二) —— 索引及切片
- Android中活动Activity方面的知识点
- openvswitch2.8.1 centos7.4 源码编译安装
- 实现自己的.NET Core配置Provider之Yaml
- 加白名单_Android保活从入门到放弃:乖乖引导用户加白名单吧
- 解决设置了display:none的元素,会先展示再隐藏
- c语言程序设计 第三版 哈工大,c语言程序设计 哈工大 苏小红 第三章习题
- 论信息化投标低于1元中标值吗?
- 【DevCloud · 敏捷智库】Scrum和看板如何选择
- .net如何获取文件夹中的文件_access递归列出文件夹中的文件
- c++如何打印一维数组首地址_4.1 数组的定义
- 【SAP技术汇】说说SAP那些事儿
- ip地址二进制转十进制
- Ns3 构建哑铃型拓扑,并实现两个点的TCP连接(详细请戳全文)
- Google 元素定制 T 恤
- windows安装JDK步骤
- 计算机信息技术知识点思维导图,思维导图信息技术的学习方法
- pmp考试中变更的处理流程
- 天津大学仁爱学院哪个计算机专业2013山西理科录取分数,天津大学仁爱学院2020年录取分数线(附2017-2020年分数线)...
- js 百分比计算公式,图形填充的百分比