题目描述:

第一次提交:

class Solution(object):
def invertTree(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
if not root:
return None
temp = root.left
root.left = root.right
root.right = temp# root.left,root.right = root.right,root.left
self.invertTree(root.left)
self.invertTree(root.right)
return root

可缩减代码:

class Solution(object):
def invertTree(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
if not root :
return root
else:
root.left,root.right=self.invertTree(root.right),self.invertTree(root.left)
return root

leetcood学习笔记-226- 翻转二叉树的更多相关文章

  1. leetcood学习笔记-965-单值二叉树

    题目描述; 第一次提交; class Solution: def isUnivalTree(self, root: TreeNode) -> bool: if root == None: ret ...

  2. leetcood学习笔记-110-平衡二叉树

    ---恢复内容开始--- 题目描述: 方法一: class Solution(object): def isBalanced(self, root): """ :type ...

  3. leetcood学习笔记-101-对称二叉树

    题目描述: 方法一:递归: class Solution: def isSymmetric(self, root: TreeNode) -> bool: if not root: return ...

  4. Java实现 LeetCode 226 翻转二叉树

    226. 翻转二叉树 翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 备注: 这个问题是受到 Max ...

  5. 力扣(LeetCode)226. 翻转二叉树

    翻转一棵二叉树. 示例: 思想 递归 java版 /** * Definition for a binary tree node. * public class TreeNode { * int va ...

  6. Leetcode题目226.翻转二叉树(简单)

    题目描述: 翻转一颗二叉树 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 思路分析: 1)递归,不断交换左右子树,直到 ...

  7. leetcood学习笔记-54-螺旋矩阵

    题目描述: 第一次提交: class Solution: def spiralOrder(self, matrix: List[List[int]]) -> List[int]: j,x = 0 ...

  8. 【LeetCode】226. 翻转二叉树

    题目 翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 本题同[剑指Offer]面试题27. 二叉树的镜 ...

  9. leetcood学习笔记-20

    python字符串与列表的相互转换   学习内容: 1.字符串转列表 2.列表转字符串 1. 字符串转列表 str1 = "hi hello world" print(str1.s ...

随机推荐

  1. list采坑记录一下

    List<Integer> cards = Lists.newArrayList(6,10,11,12,21,23,29,30,38,39,42,43,46,51,53,59,60);Li ...

  2. Java常见的开源数据连接池有哪些,并对参数做出简单的说明

    (1)DBCP DBCP是一个依赖Jakarta commons-pool对象池机制的数据库连接池.DBCP可以直接的在应用程序中使用,Tomcat的数据源使用的就是DBCP. (2)c3p0 c3p ...

  3. LayUI最近遇到的问题以及处理

    layui是我最近才接触的..也是新项目中用到的后台前端框架..与easyui有些类似..在这段时间的使用中,经常会碰到大大小小的问题.. 1.选显卡切换又是加载数据表格.分页条不显示 2.layui ...

  4. PHP ftp_exec() 函数

    定义和用法 ftp_exec() 函数请求在 FTP 服务器上执行一个程序或命令. 如果成功,该函数返回 TRUE.如果失败,则返回 FALSE. 语法 ftp_exec(ftp_connection ...

  5. 【LeetCode 6】Z 字形变换

    题目链接 [题解] 还想着模拟这个过程.然后发现只有行有用啊!... 那就建个rows大小的字符串数组存每行从左到右的字符就行啦.. 然后就是i从1变到n然后又变回1反复就好了. 最后把1..rows ...

  6. mysql|tomcat|nginx|redis在docker中的部署

    MySQL部署 拉取MySQL镜像 docker pull mysql 查看镜像 创建MySQL容器 docker run -di --name pinyougou_mysql -p 33306:33 ...

  7. centos coreseek

    下载稳定版 coreseek wget http://www.coreseek.cn/uploads/csft/3.2/coreseek-3.2.14.tar.gz 解压 .tar.gz cd cor ...

  8. 可持化永久树 的 STL ( rope )

    rope 的基本操作 #include <ext/rope> using namespace __gnu_cxx; ]; rope<int> x; rope<int> ...

  9. 如何在一个for语句中迭代多个对象(2.7)

    如何在一个for语句中迭代多个对象 总结: 并行迭代使用zip(l1, l2, l3) 每次迭代从3个列表里各取一个数据 串行迭代使用itertools.chain(l1, l2, l3) 相当于把3 ...

  10. B606 ChangeNet

    @echo off Setlocal Enabledelayedexpansion title B606 ChangeNet echo Checking... set inside=F&set ...