LeetCode 226 Invert Binary Tree 解题报告
题目要求
Invert a binary tree.
题目分析及思路
给定一棵二叉树,要求每一层的结点逆序。可以使用递归的思想将左右子树互换。
python代码
# 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
right = self.invertTree(root.right)
left = self.invertTree(root.left)
root.left = right
root.right = left
return root
LeetCode 226 Invert Binary Tree 解题报告的更多相关文章
- Java for LeetCode 226 Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia: This problem wa ...
- Java [Leetcode 226]Invert Binary Tree
题目描述: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 解题思路: 我只想说递归大法好. ...
- LeetCode 226. Invert Binary Tree (反转二叉树)
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...
- Leetcode 226. Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 class Solution(object): ...
- (easy)LeetCode 226.Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...
- Leetcode 226 Invert Binary Tree python
题目: Invert a binary tree. 翻转二叉树. 递归,每次对节点的左右节点调用invertTree函数,直到叶节点. python中也没有swap函数,当然你可以写一个,不过pyth ...
- leetcode 226 Invert Binary Tree 翻转二叉树
大牛没有能做出来的题,我们要好好做一做 Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Tri ...
- Leetcode 226. Invert Binary Tree(easy)
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...
- LeetCode (226):Invert Binary Tree 递归实现
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...
随机推荐
- 清理configure脚本生成的文件
今天在修改dovecot的代码时遇到一个问题,需要重新执行configure脚本,想先把之前configure生成的文件删除掉. 结果看了configure --help没有找到可以用的命令,最后搜了 ...
- MXNET:深度学习计算-GPU
mxnet的设备管理 MXNet 使用 context 来指定用来存储和计算的设备,例如可以是 CPU 或者 GPU.默认情况下,MXNet 会将数据创建在主内存,然后利用 CPU 来计算.在 MXN ...
- jdk和tomcat基本配置
问题:前端采用grunt构建,后台采用java编写使用Eclipse或IntelliJ,把Tomcat嵌入到开发工具当中.问题一:在于是grunt编译之后生成的文件,每次都需要刷新项目文件夹,然后在刷 ...
- last
last reboot 显示系统最后重启的历史记录 last -w 显示自系统启动以来,所有登录的用户(全名)
- Tensorflow读写TFRecords文件
在使用slim之类的tensorflow自带框架的时候一般默认的数据格式就是TFRecords,在训练的时候使用TFRecords中数据的流程如下:使用input pipeline读取tfrecord ...
- [DQN] What is Deep Reinforcement Learning
已经成为DL中专门的一派,高大上的样子 Intro: MIT 6.S191 Lecture 6: Deep Reinforcement Learning Course: CS 294: Deep Re ...
- ctrl c 中文字符到 vnc 里,中文字符已经被转码
为了测试程序对多语言字符的支持情况,我找来一段中文和北欧的文字,希望把这些文字上传到elasticsearch,并能正确显示. 首先测试了北欧文字,一切OK. 但是中文复制到 VNC 客户端(Linu ...
- 使用Postmark测试后端存储性能
Postmark用于对进行频繁,大量存取小文件的存储系统的存储性能测试.原理:构建一个测试文件池,通过文件最大,最小大小,数量等参数进行配置,然后进行事务的初始化,对每一个事务中读取/附加,创建/删除 ...
- maven子项目的springboot配置
正常来说一个maven子项目的parent是父项目,而不是直接继承,这时候就需要改下配置 默认生成的代码入下: <?xml version="1.0" encoding=&q ...
- 使用python删除一个文件或文件夹
使用python删除一个文件或文件夹,需要使用os模块. import osos.remove(path) # path是文件的路径,如果这个路径是一个文件夹,则会抛出OSError的错误,这时需用用 ...