题目:

Invert a binary tree. 翻转二叉树。

递归,每次对节点的左右节点调用invertTree函数,直到叶节点。

python中也没有swap函数,当然你可以写一个,不过python中可以通过:a, b = b, a交换两个变量的值

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

Leetcode 226 Invert Binary Tree python的更多相关文章

  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 ...

  2. 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 ...

  3. LeetCode 226 Invert Binary Tree 解题报告

    题目要求 Invert a binary tree. 题目分析及思路 给定一棵二叉树,要求每一层的结点逆序.可以使用递归的思想将左右子树互换. python代码 # Definition for a ...

  4. 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): ...

  5. 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 ...

  6. (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 ...

  7. 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 解题思路: 我只想说递归大法好. ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. js编程风格

    1.缩进层级,建议四个空格. 2.语句结尾使用分号. 3.行的长度不超过80个字符. 4.换行建议加两个缩进,即8个空格. 5.合理的利用空行. 6.命名: 6.1 变量,驼峰式大小写,有小写字母开始 ...

  2. 学习第一个头文件stdio.h

    使用标准输入输出库函数时要用到 “stdio.h”文件,因此源文件开头应有以下预编译命令: #include<stdio.h> stdio是standard input&outup ...

  3. X-factor Chains(POJ3421 素数)

    X-factor Chains Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6212   Accepted: 1928 D ...

  4. c#以文件流的形式输出xml(可以解决内存溢出)-XmlTextWriter

    1.XmlTextWriter 表示提供快速.非缓存.只进方法的编写器,该方法生成包含 XML 数据(这些数据符合 W3C 可扩展标记语言 (XML) 1.0 和“XML 中的命名空间”建议)的流或文 ...

  5. typedef函数指针那些事

    首先来介绍下函数指针: 函数指针是指向函数的指针变量,即本质是一个指针变量. int (*f) (int x); /* 声明一个函数指针 */ f=func; /* 将func函数的首地址赋给指针f ...

  6. Android DatePickerDialog 只选择年月

    //对EditText注册OnTouch事件etSscxNssbDate.setOnTouchListener(selectDateTouchListener); //选择日期 private OnT ...

  7. SQL Server - 聚集索引 <第六篇>

    聚集索引的叶子页存储的就是表的数据.因此,表行物理上按照聚集索引列排序,因为表数据只能有一种物理顺序,所以一个表只能有一个聚集索引. 当我们创建主键约束时,如果不存在聚集索引并且该索引没有被明确指定为 ...

  8. BufferedStream类 - 缓冲流

    BufferedStream常用于对其他流的一个封装,它必须和其他流结合一起使用.MemoryStream将所有的内容都放入内存中,而BufferedStream不是.BufferedStream在基 ...

  9. scheme I/0 输入输出操作

    2.1. open-input-file, read-char, and eof-object? The function (open-input-file filename) is availabl ...

  10. 【POJ 1330 Nearest Common Ancestors】LCA问题 Tarjan算法

    题目链接:http://poj.org/problem?id=1330 题意:给定一个n个节点的有根树,以及树中的两个节点u,v,求u,v的最近公共祖先. 数据范围:n [2, 10000] 思路:从 ...