[leetcode] 226. Invert Binary Tree @ python

LeetCode --- 226. Invert Binary Tree解题报告 Invert a binary tree. Example: Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia: This problem was inspired bythis origin... 阅读详情

原题

Invert a binary tree.

Example:

Input:

 4

/
2 7
/ \ /
1 3 6 9
Output:

 4

/
7 2
/ \ /
9 6 3 1
Trivia:
This problem was inspired by this original tweet by Max Howell:

Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so f*** off.

解法1

递归

代码

# 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
        
        root.left, root.right = self.invertTree(root.right), self.invertTree(root.left)
        return root

解法2

DFS

代码

# 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':
        q = [root]
        while q:
            node = q.pop()
            if node:
                node.left, node.right = node.right, node.left
                q.extend([node.left, node.right])
        return root
国科大人工智能原理与算法-2024-第一次作业-雷震老师 1. 反射行动(比如从热炉子上缩回你的手)是理性的吗?它们是智能的吗? 2. 以下的计算机系统在何种程度上是人工智能的实例: - 超市条码扫描器 - 网络搜索引擎 - 语音激活的电话菜单 - 对网络状态动态响应的网络路由算法 3. 为什么进化会倾向于导致行为合理的系统?设计这样的系统想达到的目标是什么? 阅读详情

相关推荐

从PHY芯片看工业网络精准时钟:IEEE 1588v2(PTP)协议实现与选型指南

本文深入解析了工业网络中IEEE 1588v2(PTP)协议的实现与PHY芯片选型要点。通过对比KG7101、RTL8211F等芯片的关键参数,揭示硬件时间戳对纳秒级时钟同步的决定性作用,并提供PTP网络设计中的拓扑布局与配置优化技巧,助力工业自动化场景实现精准时间同步。

weixin_42628688的博客 513

226. Invert Binary Tree [easy] (Python)

题目链接https://leetcode.com/problems/invert-binary-tree/题目原文 Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia: Thi

Coder_Orz的博客 4940

Pixhawk固定翼飞行控制实战:L1与TECS算法详解(附代码解析)

本文深入解析了Pixhawk飞控在固定翼无人机中的核心控制算法:L1导航与TECS总能量控制。通过剖析算法原理、代码实现与实战调参,帮助开发者解决航线跟踪振荡、高度速度不稳定等常见问题,实现精准稳定的自主飞行。

wine的专栏 750

leetcode 226. Invert Binary Tree (python)

leetcode 226. Invert Binary Tree题目解法1:利用递归(recursive)形式1形式2(跟简洁,直接用原函数进行递归)解法2:利用迭代(iterative) 题目 Invert a binary tree.(具体题目参考leetcode官网) 解法1:利用递归(recursive) 递归的将每个节点的左右节点调换 形式1 class Solution(object)...

qq_37821701的博客 451

Windows C语言编程:Beep API实现蜂鸣器原理与实战

如果你在 Windows 上写过 C 语言程序,想给程序加个简单的提示音,第一个想到的可能是 printf("\a") 。这个转义字符确实能在某些终端里发出“嘀”的一声,但它太原始、太不可控了,而且很多时候根本没声音。那么,有没有一种更“Windows”、更可控、更专业的方法来让你的程序“说话”呢? 答案是肯定的,而且它比你想象的要强大和复杂。Windows 提供了一个名为 Beep 的 API 函数,专门用于生成蜂鸣声。你可能觉得这功能太简单,甚至有点过时——不就是让电脑“哔”一声吗?但恰恰是这个看似简单

weixin_34109408的博客 402

Verilog实战:手把手教你实现IEEE 754单精度浮点乘法器(附完整代码)

本文详细介绍了如何使用Verilog实现IEEE 754单精度浮点乘法器,包括标准解析、架构设计、Verilog代码实现及优化技巧。通过两级流水线设计,平衡性能与面积,适用于数字信号处理、AI加速器等高性能计算场景。

weixin_33724046的博客 317

LeetCode:226. Invert Binary Tree

LeetCode:226. Invert Binary Tree Invert a binary tree. Example: Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: 4 / \ 7 2 / \ / \ 9 6 3 1 翻转二叉树 思路:递归翻转左右子树 ...

Wisimer 201

泛微OAe9-自定义资源看板

OA中,有一个很好用的功能:会议室预约。对于一间设立的会议室,同一时间段内,肯定会有不同的人预约同一间会议室,并且,在资源看板里,能看清各个时间段会议室的占用情况,可是会议室资源是有限的,显然不可能让多个人在同一时间段内预约同一间会议室,如会议室已占用,其他人再次申请时,应当拒绝预约。

小松鼠的博客 1863

Leetcode 226. Invert Binary Tree (python+cpp)

Leetcode 226. Invert Binary Tree题目解法:递归 题目 解法:递归 先交换,再递归 python class Solution: def invertTree(self, root: TreeNode) -> TreeNode: if not root: return root.left,root.right = root.right,root.left self.invertTree(ro

qq_37821701的博客 164

SAP Workflow 工作流开发步骤总结

SAP WORKFLOW

qq_36952606的博客 5108

低功耗设计-ir drop的signoff corner怎么选择?

2.tt的(tt85 是比较接近芯片真实工作情况的);我正在「拾陆楼」和朋友们讨论有趣的话题,你⼀起来吧?有要求就用要求的就好,没要求前两种看公司需求。3.signoff文档要求的;有几个方向,看公司需求吧。

m0_61544122的博客 1611

python leetcode 226. Invert Binary Tree

Invert a binary tree.二叉树反转Example:Input: 4 / \ 2 7 / \ / \ 1 3 6 9Output: 4 / \ 7 2 / \ / \ 9 6 3 1# Definition for a binary tree node.# class TreeNode:#   ...

DreamerLHQ的博客 499

【知识图谱】Neo4j基本操作及数据库文件导入(graph.db.dump)

初探Neo4j导入数据备份数据客户端操作导入csv文件指定csv文件格式使用命令行导入csv文件附录 请读者参考上篇博文 Neo4j安装教程 导入数据 若开启服务,先在终端停止 Neo4j 服务 ./neo4j stop 接下来,下载射雕三部曲的数据库文件 wget https://labfile.oss.aliyuncs.com/courses/1354/graph.db.dump 下载完将出现 graph.db.dump 文件: 将数据库文件导入到 Neo4j 中 首先进入neo4j 根目录下:

全网粉丝30W+,CSDN、稀土掘金人工智能领域签约作者,人工智能、大模型、Python 爬虫,商务合作,请私信沟通。 8628

[LeetCode]226. Invert Binary Tree ★

每天一道编程题题目描述样例python解法C语言解法 题目描述 Invert a binary tree. 题目大意:交换二叉树的左右子树 样例 Example: Input: Output: python解法 # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # ...

xingyu97的博客 171

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

Leetcode226. Invert Binary Tree

文章目录链接思路cpp1.2.3.python 链接 link 思路 感觉应该是用递归来做。每次反转当前结点下的两个,再反转那两个结点下的。 cpp 1. 因为用了递归所以感觉不是很难。 刚开始尝试不用子函数来做,但是这个主函数要求,每次必须返回一个TreeNode *。就很麻烦。所以另外写了一个不用返回的函数。 /** * Definition for a binary tree node. ...

weixin_44814121的博客 128

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

LeetCode---226. Invert Binary Tree

题目 给出一个二叉树,将其左右节点翻转。 Python题解一:递归 class Solution(object): def invertTree(self, root): """ :type root: TreeNode :rtype: TreeNode """ if root is None: ...

coderate 318

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

leetcode-226-Invert Binary Tree

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 inspired by this orig

早睡早起好少年 451

Leetcode Invert Binary Tree python 反转二叉树的问题 还是DFS和BFS,递归和迭代的操作

Leetcode 226题 Invert Binary Tree Invert a binary tree. Example: Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: 4 / \ 7 2 / \ / \ 9 6 3 1 据说是谷歌面试题。 很简单了,二叉树的题...

润泽的博客 740

leetcode 654. Maximum Binary Tree(构建最大二叉树)

题目要求 给定一个没有重复的整数数组。 此阵列上的最大树构建定义如下: 1、根是数组中的最大数字。 2、左子树是根的左部分子数组构造的最大树。 3、右子树是根的右部分子数组构造的最大树。 4、通过给定数组构造最大树并输出此树的根节点。 输入实例 Example 1: Input: [3,2,1,6,0,5] Output: return the tree root node representi...

爱喝舒化奶的博客 468

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 inspired

shiter编写程序的艺术 4300

LeetCode 226. Invert Binary Tree(Python)

题目描述: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9to 4 / \ 7 2 / \ / \ 9 6 3 1思路: 运用递归前序遍历二叉树来实现交换AC代码:class Solution(object): def invertTree(self,

Lu_gee的博客 552

[Leetcode 226, Easy] Invert Binary Tree

[Leetcode 226, Easy] Invert Binary Tree

算法的实践 498

leetcode python3 简单题226. Invert Binary Tree

1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百二十六题 (1)题目 英文: Invert a binary tree. 中文: 翻转一棵二叉树。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/invert-binary-tree (2)解法 ① 递归 (耗时:44ms,内存:13.5M) # Definition for a binary tree node. # class

从此做一个俗人 310

[leetcode] 226. Invert Binary Tree

Description Invert a binary tree. Example: Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia: This problem was inspired by this ...

Learning from the mistakes 167

leetcode 226. Invert Binary Tree 翻转二叉树 python3

时间:2020-6-11 题目地址:https://leetcode-cn.com/problems/invert-binary-tree/ 题目难度:Easy 题目描述: 翻转一棵二叉树。 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 备注: 这个问题是受到 Max Howell 的 原问题 启发的 : 谷歌:我们9...

isabloomingtree的博客 280

【Leetcode】226. Invert Binary Tree 解题报告

头条面试问到的原题 交换二叉树的左右子树: 方法1 递归 代码简单易写,但面试的时候写出递归面试官肯定会让你写非递归的 class Solution(object): def invertTree(self, root): """ :type root: TreeNode :rtype: TreeNode """ ...

很吵请安静 399
上一篇: [leetcode] 682. Baseball Game @ python
下一篇: [leetcode] 937. Reorder Log Files @ python
闲庭信步的空间
博客等级 码龄9年 86粉丝 416原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值