题目要求

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 解题报告的更多相关文章

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

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

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

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

  6. Leetcode 226 Invert Binary Tree python

    题目: Invert a binary tree. 翻转二叉树. 递归,每次对节点的左右节点调用invertTree函数,直到叶节点. python中也没有swap函数,当然你可以写一个,不过pyth ...

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

  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. pandas遍历行数据

    假设我的DataFrame如图所示: 我可以这样遍历它: for index,row in list.iterrows(): id =row["id"] x=row["x ...

  2. Geany的"跳转到标记定义“功能如何使用

    Geany是个比较轻量级的代码编辑器,在一些不怎么需要编辑的代码上,我比较常用它来浏览代码.不过它的 跳转到标记定义(Go to tag definition) 功能有点奇怪,一开始死活不知道怎么用, ...

  3. 【GMT43智能液晶模块】例程二:串口通信实验

    实验原理: GMT43智能液晶模块的串口包括USB_UART(CH340),TTL,RS-232,RS-485/ RS-422等四部分,USB_UART部分通过CH340芯片与STM32F429的US ...

  4. 树莓派做路由器_配置防火墙filter和nat转发_转载

    转自:https://blog.csdn.net/hustsselbj/article/details/45866681 如果用树莓派当作路由器转发有线和无线网络,则需要对iptables进行相关配置 ...

  5. 解决:android源码同步repo sync 时出现的fatal:duplicate path错误

    问题重现: 解决方法: 1.删除android项目里隐藏的 .repo 文件夹中除了以下几个文件夹的其他文件及文件夹 2.重新初始化android项目 repo init -u https//gith ...

  6. Pwnium CTF2014 – MatterOfCombination writeup

    这道题是虽然只有75分,但是做出来的队伍却很少,我们队伍也没有做出来,这次是看到了0xAWES0ME 的解题思路后才有了这篇文章.原文地址可以点击看这里,英文的. 题目就是一张图片: 在网上可以找到这 ...

  7. Java知多少(29)覆盖和重载

    在类继承中,子类可以修改从父类继承来的方法,也就是说子类能创建一个与父类方法有不同功能的方法,但具有相同的名称.返回值类型.参数列表. 如果在新类中定义一个方法,其名称.返回值类型和参数列表正好与父类 ...

  8. linux-centos下持续集成工具jenkins部署使用教程

    centos下准备工作: 1.安装jdk环境 2.安装maven环境(用于构建项目) 3.git客户端(用于拉取源码) 此外码云还要准备一份java源码(springboot项目) 一.安装jenki ...

  9. [JS] ECMAScript 6 - Variable : compare with c#

    前言 范围包括:ECMAScript 新功能以及对象. 当前的主要目的就是,JS的学习 --> ECMAScript 6 入门 let 命令 js 因为let, i的范围限制在了循环中. var ...

  10. 解决VisualStudio无法调试的问题

    方法1 方法2