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 ...
随机推荐
- OGG-01028 Incompatible Record解决办法
How to recover from an OGG-01028 Incompatible Record if the trail is not corrupt (Doc ID 1507462.1) ...
- LeetCode: Best Time to Buy and Sell Stock III 解题报告
Best Time to Buy and Sell Stock IIIQuestion SolutionSay you have an array for which the ith element ...
- 【emWin】例程二十七:窗口对象——Listview
简介: LISTVIEW小工具可在具有多个列的列表中选择某个元素.由于LISTVIEW小工具包含了 一个HEADER小工具,因此可对列加以管理(排序等).所创建的LISTVIEW 既可以无环绕型框架窗 ...
- EntLib 自动数据库连接字符串加密
const string provider = "RsaProtectedConfigurationProvider"; Configuration config = null; ...
- moment.js返回本周
项目中需要做个打卡的模块.里面有个模块需要返回当前这个星期从星期日到星期六的日期,如下图: 我是通过 moment.js 的 moment().day() 实现这个效果的,它的说明如下图: 关于这个插 ...
- Ubuntu 设置NAT共享网络(命令行方法)
本文介绍如何使用iptables来实现NAT转发,事实上就是将一台机器作为网关(gateway)来使用.我们假设充当网关的机器至少有网卡eth0和eth1,使用eth0表示连接到外网的网卡,使用eth ...
- 【QT】第一个QT程序(点击按钮,显示特定文本)
1.基类选 QWidget 2.添加UI Btnshowhello 按钮 labelhello 文本标签 要注意:文本字样和对象名的区别! 对象名和UI部件是一一对应的关系. 修改对象名和修改文本字样 ...
- 网络编程 -- RPC实现原理 -- RPC -- 迭代版本V2 -- 本地方法调用 整合 Spring
网络编程 -- RPC实现原理 -- 目录 啦啦啦 V2——RPC -- 本地方法调用 + Spring 1. 配置applicationContext.xml文件 注入 bean 及 管理 bean ...
- iOS开发-- 开发中遇到的问题汇总
1. CUICatalog: Invalid asset name supplied: 今天写了加载图片,默认图片写的是[UIImage imageNamed:@""],之后就报下 ...
- fs项目---->migrate-mongo的使用(一)
tw项目中用的是mongo数据库,数据的迁移也是需求的一部分.这时我们可以使用migrate-mongo在nodejs中方便的进行数据的迁移,以下记录一下使用的过程. 一.migrate-mongo的 ...