[Leetcode]156.Binary Tree Upside Down

LeetCode - #156 上下翻转二叉树(会员题) 本题为 LeetCode 的高级会员解锁题我们社区陆续会将顾毅(Netflix 增长黑客,《iOS 面试之道》作者,ACE 职业健身教练。)的 Swift 算法题题解整理为文字版以方便大家学习与阅读。LeetCode 算法到目前我们已经更新到 155 期,我们会保持更新时间和进度(周一、周三、周五早上 9:00 发布),每期的内容不多,我们希望大家可以在上班路上阅读,长久积累会有很大提升。不积跬步,无以至千里;不积小流,无以成江海,Swift社区 伴你前行。 阅读详情

原题链接:https://leetcode.com/problems/binary-tree-upside-down/

这是一道带锁题,原题是说给定一个二叉树,二叉树的右子树要么是空的,要么是有一个兄弟节点的叶子子树(没有孩子)。题目要求讲这课二叉树上下翻转一下,并且使得二叉树的右节点变成左节点。


解:

这个树是一棵有特征的树,它的生长是沿着左子树方向的。所以我们只需定义好几个变量,沿着左子树将每个节点迭代处理即可:假设node,将node的右子树设为原树的parent节点,讲node的左子树设为原树中的兄弟右节点即可。时间复杂度为O(h),h为树的高度。代码如下:

public TreeNode upsideDownBinaryTree(TreeNode root) {

        if(root == null)

            return null;

        TreeNode parent = null;

        TreeNode node = root;

        TreeNode right = null;

        while(node!=null)

        {

            TreeNode left = node.left;

            node.left = right;

            right = node.right;

            node.right = parent;

            parent = node;

            node = left;

        }

        return parent;

    } 
遇到树的问题,主要还是用DFS来解,这题比较特殊
【ROS机器人系统】实验4 ROS简单应用:激光SLAM与导航 ROS简单应用:激光SLAM与导航 (1)了解机器人和SLAM之间关系 (2)了解激光SLAM (3)了解gmapping和导航 阅读详情

相关推荐

地瓜派RDK X5部署YOLOv11n避坑指南:从Softmax瓶颈到BPU加速的实战解析

本文详细解析了在地瓜派RDK X5开发板上部署YOLOv11n模型时,从性能暴跌到成功优化的全过程。核心在于通过配置`node_info`将模型中的Softmax算子强制在BPU上以int16精度运行,解决了因算子被分配到CPU导致的严重性能瓶颈。实战部署后,模型推理延迟从126ms降至10.8ms,FPS从7.9提升至92.3,实现了显著的性能优化。

beta5的博客 865

LeetCode第156题_上下翻转二叉树

给定一个二叉树,其中所有的右节点要么是具有兄弟节点(拥有相同父节点的左节点)的叶子节点,要么为空,将此二叉树上下翻转并将它变成一棵树,原来的右节点将转换成左叶子节点。返回新的根。

qq_40263592的博客 719

碳纳米管NRAM:突破5纳米制程的下一代存储技术

在半导体存储领域,非易失性存储器和随机存取存储器是两大基础技术方向。传统DRAM速度快但断电易失,NAND Flash非易失但写入慢、寿命有限,两者在先进制程下都面临物理极限挑战。碳纳米管作为一种新型纳米材料,凭借其优异的导电性、机械强度和尺寸微缩潜力,为存储技术革新提供了物理基础。基于碳纳米管的NRAM技术,通过控制纳米管间的可逆接触实现电阻状态切换,兼具DRAM级速度和Flash的非易失性,同时具备近乎无限的擦写寿命和出色的CMOS工艺兼容性。这项技术不仅能满足5纳米以下制程的存储需求,在数据中心缓存、

weixin_42576804的博客 427

【LeetCode刷题-树】-- 156.上下翻转二叉树

LeetCode刷题

qq_46656857的博客 222

RT_Thread Studio使用——USB虚拟串口(VCP)

RT_Thread Studio使用——USB虚拟串口(VCP)

weixin_44735205的博客 3373

电动汽车BMS核心技术:AS8510高精度传感器解析

电池管理系统(BMS)是电动汽车的核心组件,其精度直接影响电池安全与续航表现。通过Σ-Δ ADC和斩波稳定等技术,现代BMS传感器可实现±0.5%的电流测量精度,满足汽车级AEC-Q100和ISO 26262标准。AS8510作为行业标杆芯片,采用双通道16位ADC架构,在-40°C至+150°C范围内保持稳定性能,特别适合新能源汽车的严苛环境。这类高精度传感器技术不仅解决了传统BMS的温漂难题,还为智能电池诊断、无线BMS等创新应用奠定了基础,是推动电动汽车技术进步的关键要素。

weixin_30781775的博客 533

【LeetCode】156.Binary Tree Upside Down(Medium)(加锁题)解题报告

【LeetCode】156.Binary Tree Upside Down(Medium)(加锁题)解题报告题目地址:https://leetcode.com/problems/binary-tree-upside-down/ 题目描述:   Given a binary tree where all the right nodes are either leaf nodes with a s

郝春雨的博客 343

高效打卡系统设计:从行为心理学到实践工具

打卡系统作为现代时间管理的重要工具,其核心原理植根于行为心理学中的即时反馈机制和习惯养成理论。通过科学设计的打卡周期和可视化反馈,能有效提升个人自律性和目标达成率。在技术实现上,从纸质记录到数字化工具(如Notion、Excel自动化模板)的演进,为不同场景提供了灵活解决方案。特别在习惯追踪和进度管理领域,结合热力图生成、数据统计等技巧,打卡系统能转化为强大的自我监督工具。本文通过实践案例,详解如何构建兼顾弹性和约束力的个性化打卡方案,帮助读者突破'三天打鱼两天晒网'的常见困境。

weixin_30047059的博客 335

leetcode156上下翻二叉树

题目描述 给定一个二叉树,其中所有的右节点要么是具有兄弟节点(拥有相同父节点的左节点)的叶节点,要么为空,将此二叉树上下翻转并将它变成一棵树, 原来的右节点将转换成左叶节点。返回新的根。 解题思路 把树倒过来看 对于任意一个节点,它的右节点变成左孩子的左孩子,自己变成左孩子的右孩子。对于每一个节点的左孩子重新设置左右节点。 定义一个dfs函数,功能如下 1. node为null,则直接return 2. node为叶子节点 如果head为空,则head = node(...

weixin_37841366的博客 2169

一文解读DeepSeek训练数据集

本文重点以DeepSeek为例,详细介绍预训练数据集相关内容。大语言模型LLM(如DeepSeek、ChatGPT、文心一言等)的“智慧”源于它们学习的海量数据集。简单来说,数据集就是模型的“知识库”,通常由互联网公开的文本、图片、视频等。这些数据需满足两个特点:规模大和多样性。规模大是指数据量可达千亿级词汇,让模型学习复杂规律;多样性则涵盖多领域(如科技、文学)、多语言、多形式(对话、新闻),帮助模型适应不同场景。例如,训练数据中既有百科知识,也有日常对话,模型才能既解答专业问题,又能闲聊。

u013891230的博客 7080

R数据分析:广义估计方程式GEE的做法和解释

好久没有更新文章了,因为同学们咨询的问题有点多,另一个原因就是自己实在太懒。。。。 今天继续给大家写广义估计方程式GEE。 In statistics, a generalized estimating equation (GEE) is used to estimate the parameters of a generalized linear model with a possible unknown correlation between outcomes. 上面的英文解释告诉我们,当我们

Codewar的博客 1万+

LeetCode 156 Binary Tree Upside Down 上下翻转二叉树

本文转载自:https://segmentfault.com/a/1190000016755610 LeetCode 156 Binary Tree Upside Down Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares ...

Umbrella Corporation 9973

OCR技术解析:用Tesseract和PaddleOCR识别文本

OCR技术解析:用Tesseract和PaddleOCR识别文本 ,人工智能,计算机视觉,大模型,AI,光学字符识别(Optical Character Recognition,简称 OCR)是一项将图片、扫描件等文档中的文字信息转换为可编辑文本的技术。在数字化时代,OCR 技术广泛应用于文档处理、车牌识别、身份证识别、古籍数字化等领域,极大地提高了文字信息处理的效率。Tesseract 和 PaddleOCR 是两款极具代表性的 OCR 工具。

不踩坑,不知道坑有多深;不总结,踩过的坑白踩。 3万+

leetcode 156: Binary Tree Upside Down

Binary Tree Upside Down Total Accepted: 813 Total Submissions: 2515 Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent

西施豆腐渣 3949

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

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

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

[LeetCode 156] Binary Tree Upside Down

Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tree where the origin

coder 进阶的专栏 778

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

LeetCode—156. Binary Tree Upside Down

Binary Tree Upside Down思路:显然是递归去做的 GitHub地址:https://github.com/corpsepiges/leetcode 点此进入如果可以的话,请点一下star,谢谢。 /** * Definition for a binary tree node. * public class TreeNod

徐玄清的专栏 652

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

leetcode[156] binary tree upside down

这题要收费了。只能网上看题目,我等屌丝也没法OJ测试了。网上看了后发现其实并非独创,其他的方也有类似的题。例如在这里,CareerCup上 先用了递归的想法, TreeNode *ans; TreeNode *helper156(TreeNode *root) { if (!root -> left && !ro...

159

[leetcode 156]Binary Tree Upside Down

Question: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tree w

清空 564

[leetcode] 156. Binary Tree Upside Down 解题报告

题目链接: https://leetcode.com/problems/binary-tree-upside-down/ Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or e

小榕流光的专栏 3239

【LeetCode】156. Binary Tree Upside Down

解法: 在每个节点p,使节点p的左子树根节点为该节点右兄弟节点,节点p的右子树根节点为该节点的父节点。即可。类似反转链表。 Python源码: Runtime:28 ms, faster than5.57%ofPythononline submissions forBinary Tree Upside Down. Memory Usage:11.8 MB, less ...

weixin_42344158的博客 311

[LeetCode156] Binary Tree Upside Down

Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tree where the origin

coder 进阶的专栏 789

LeetCode 156. Binary Tree Upside Down(树的倒转)

原题网址:https://leetcode.com/problems/binary-tree-upside-down/ Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or em

James Pan 991

【Leetcode】156. Binary Tree Upside Down

题目地址: https://leetcode.com/problems/binary-tree-upside-down/ 给定一棵二叉树,要求对其从树根出发的左链做如下图的操作。返回新二叉树。 思路是DFS。在DFS的时候要把节点父亲作为参数传下去。先递归处理左子树,如果遇到没有左孩子的节点,那么它就是新树根,把它存一下。然后处理当前节点,其左孩子是其父亲的右孩子,然后其右孩子就是父亲。最后原树根就是叶子了,还需要把它的左右孩子置为null。最后返回新树根即可。代码如下: public class Sol

数学、算法爱好者的博客 425

【LeetCode】156. Binary Tree Upside Down 解题报告(C++)

作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录题目描述题目大意解题方法递归迭代日期 题目地址:https://leetcode-cn.com/problems/binary-tree-upside-down/ 题目描述 Given a binary tree where all the right nodes are eith...

负雪明烛 817

LeetCode #156. Binary Tree Upside Down

题目描述: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tree where the...

LawFile的博客 202

LeetCode 156. Binary Tree Upside Down

Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tree where the origina

xinqrs01的博客 318
上一篇: 开篇
下一篇: [LeetCode] 157. Read N Characters Given Read4
foolnote
博客等级 码龄11年 0粉丝 10原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值