LeetCode OJ 226. Invert Binary Tree

从数学到机床:RTCP背后的几何变换与工业实现 本文深入解析RTCP(Rotary Tool Center Point)技术在五轴数控加工中的核心作用,通过几何变换与运动学原理实现刀尖点跟随,确保加工精度。从数学基础到工业实现,详细探讨了算法架构、机床结构适配及精度保障方法,为数控系统开发和高效加工提供关键技术支撑。 阅读详情

LeetCode OJ 226. Invert Binary Tree


Description

Invert a binary tree.

     4
   /   \
  2     7
 / \   / \
1   3  6  9

to

     4
   /   \
  7     2
 / \   / \
9   6  3  1

方法一

Recursion: 递归,每一次翻转左右子树。这种方法要每次保存左右子树的根节点,然后再把翻转的左右子树根节点分别赋值给父节点的右子节点和左子节点。

代码


/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if(!root)
           return root;
        TreeNode *l, *r;
        l = invertTree(root->left);
        r = invertTree(root->right);
        root->left = r;
        root->right = l;
        return root;
    }
};

方法二

遍历(queue实现): 对于每一个节点,都进行遍历,并且交换该节点的左右子节点。

代码

个人github代码链接


/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if(!root)
           return root;

        queue<TreeNode *> q;
        q.push(root);
        while(!q.empty()){
            TreeNode *node = q.front();
            q.pop();
            TreeNode * temp = node->left;
            node->left = node->right;
            node->right = temp;
            if(node->left)  q.push(node->left);
            if(node->right) q.push(node->right);
        }
        return root;
    }
};
如何在 Ubuntu 22.04 上安装和配置 VNC 服务器? VNC(Virtual Network Computing)是一种图形桌面共享系统,通过网络允许用户控制另一台计算机的桌面环境。无论你是系统管理员还是开发人员,VNC 都可以在远程访问桌面环境时提供极大的便利。在安装和配置 VNC 服务器之前,我们需要确保系统是最新的,并安装一些基本工具。这些命令将更新你的软件包列表并升级所有已安装的软件包,以确保你拥有最新的安全修复和功能更新。 阅读详情

相关推荐

【MFiX后处理】MFiX中使用des_usr_var输出颗粒的传热量(2020-11-27更新变更输出变量名方法)

参考之前Yin的那一篇博客 https://www.cnblogs.com/Jay-CFD/p/10621274.html 输出一下颗粒传热量 使用的算例是fluid_bed_dem_2d 打开des scalar tracking 前面都和Yin那篇一样,打开enable user scalar tracking 输入2 因为第一个用来测试Y加速度了 第二个输出传热量 然后在output vtk中勾选好 更改源代码 源代码为des_thermo_newvalues.f 在mfix-GUI自带的ed

beidou111的博客 1540

[leetcode oj 226]Invert Binary Tree

Invert Binary Tree

fukyoucs的专栏 246

从零构建AI辅助逆向分析环境:JADX-MCP与LLM的实战集成指南

本文详细介绍了如何从零构建AI辅助逆向分析环境,通过集成JADX-MCP与LLM技术,提升Android应用逆向工程效率。文章涵盖环境搭建、插件安装、MCP服务器配置及实战案例分析,帮助开发者快速掌握AI在代码理解、漏洞挖掘和自动化文档生成中的应用。

weixin_27875131的博客 846

LeetCode oj 226. Invert Binary Tree (DFS||BFS)

226. Invert Binary Tree   Question Editorial Solution  My Submissions Total Accepted: 124813Total Submissions: 257083Difficulty: Easy Invert a binary tree. 4 / \

Tristeza的博客 654

Innovus Function ECO实战:5步搞定数字后端设计中的网表修正(含Calibre LVS验证技巧)

本文提供了一份完整的Innovus Function ECO实战指南,详细解析了数字后端设计中网表修正的五步流程。从理解Function ECO概念、准备关键文件,到在Innovus中执行逻辑比对、自动布线及GUI检查,并重点攻克了ECO后Calibre LVS验证的难点与调试技巧,帮助工程师高效、精准地完成流片前的设计微调。

day7的博客 959

Android应用之【巧妙隐藏桌面图标的进阶方案】

本文详细探讨了Android应用中隐藏桌面图标的进阶方案,解决了传统方法在Android 10及以上版本中出现的黑色图标残留问题。通过Intent-Filter配置和厂商适配技巧,实现子应用真正隐身,同时确保主应用正常启动。适用于企业工具套件、模块化系统等场景,提升用户体验和安全性。

weixin_28363123的博客 523

LeetCode OJ-226. Invert Binary Tree(反转二叉搜索树)

226. Invert Binary Tree Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 实际上就是将节点的左右子树交换位置,代码如下: /** * Definiti

oYinGui1 684

ArcGIS Enterprise10.9.1的Windows安装部署

ArcGIS Enterprise10.9.1中Windows版本安装介绍。

qq_36250702的博客 2453

LeetCode OJ 451. Sort Characters By Frequency

LeetCode OJ 451. Sort Characters By FrequencyDescriptionGiven a string, sort it in decreasing order based on the frequency of characters.Example1 Input: “tree” Output: “eert” Explanat

jan_deng的博客 507

华为擎云W515 PGUV-WBY0安装银河麒麟V10全流程解析

本文详细解析了华为擎云W515 PGUV-WBY0安装银河麒麟V10系统的全流程,包括准备工作、镜像下载与校验、启动盘制作、BIOS设置、安装过程关键选择、系统初始化及常见问题排查。特别针对ARM架构设备提供了实用技巧,帮助用户顺利完成国产化系统的部署与优化。

weixin_28725949的博客 712

Sigrity System Explorer ViaWizard模式进行过孔建模和仿真分析操作指导

点击TVac_lower tdvac_lower,自动跳转到2D时域曲线,查看最小低电平穿过VIL(ac)max电压的延时,结果如下。点击TVac_high tdvac_high,自动跳转到2D时域曲线,查看最小高电平穿过VIH(ac)min电压的延时,结果如下。点击max Vix_Rise,自动跳转到2D时域曲线,查看上升边PN交叉点与参考电压VREF(dc)的偏差,结果如下。Threshold:阈值,软件自带3个模板,default,Tr01,Tr02,本例选择Default。

weixin_54787054的博客 579

LeetCode OJ 106. Construct Binary Tree from Inorder and Postorder Traversal

LeetCode OJ 106. Construct Binary Tree from Inorder and Postorder TraversalDescriptionGiven inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do n

jan_deng的博客 488

电阻基础知识及应用电路仿真(Multisim)

本文围绕硬件工程师必备的电阻知识,从基础知识、实操、应用仿真三方面做系统讲解:基础上明确电阻的核心参数、分类、串并联计算及核心特性;实操中详解贴片/色环电阻的阻值读取、贴片封装换算,重点区分0Ω电阻(贴装跳线,实现功能选通/调试等)与磁珠(高频吸噪)的差异及选型;应用上介绍限流、分压、上拉/下拉、阻抗匹配、过压/过流保护五大典型电路的设计公式、选型原则和实操要点,同时给出电阻通用的选型、测量和布线设计规范,形成完整的电阻应用知识体系。

XU157303764的博客 2797

LeetCode OJ 12. Integer to Roman

LeetCode OJ 513. Find Bottom Left Tree ValueDescriptionGiven an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.解题思路整数转换为罗马数字,用一个二维数组保存1-9,10-90,100-900

jan_deng的博客 441

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

LeetCode OJ 515. Find Largest Value in Each Tree Row

LeetCode OJ 515. Find Largest Value in Each Tree RowDescriptionYou need to find the largest value in each row of a binary tree.Example Input: 1 / \ 3 2 / \ \ 5 3 9 O

jan_deng的博客 432

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

LeetCode OJ 207. Course Schedule

LeetCode OJ 207. Course Schedule典型的拓扑排序算法。谨以此题纪念期末机考时没AC这么简单的拓扑排序。upset……ingDescriptionThere are a total of n courses you have to take, labeled from 0 to n - 1.Some courses may have prerequisites, for

jan_deng的博客 373

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

LeetCode OJ 240. Search a 2D Matrix II

LeetCode OJ 240. Search a 2D Matrix IIDescriptionWrite an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in

jan_deng的博客 363

LeetCode OJ 13. Roman to Integer

LeetCode OJ 13. Roman to IntegerDescriptionGiven a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.解题思路罗马数字转换为整数:我们把1,10,100,1000,5,500,500和对应的罗马数字做映射。

jan_deng的博客 356

LeetCode OJ 566. Reshape the Matrix

LeetCode OJ 566. Reshape the MatrixDescriptionIn MATLAB, there is a very useful function called ‘reshape’, which can reshape a matrix into a new one with different size but keep its original data.You’r

jan_deng的博客 332

LeetCode OJ 155. Min Stack

LeetCode OJ 155. Min StackDescriptionDesign a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) – Push element x onto stack. pop() – Removes the element o

jan_deng的博客 330

LeetCode OJ 105. Construct Binary Tree from Preorder and Inorder Traversal

LeetCode OJ 105. Construct Binary Tree from Preorder and Inorder TraversalDescriptionGiven preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not

jan_deng的博客 324

LeetCode OJ 496. Next Greater Element I

LeetCode OJ 513. Find Bottom Left Tree ValueDescriptionYou are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nu

jan_deng的博客 313

LeetCode OJ 141. Linked List Cycle

LeetCode OJ 141. Linked List CycleDescriptionGiven a linked list, determine if it has a cycle in it.Follow up: Can you solve it without using extra space?解题思路对于判断环的问题,可以采用双指针(快慢指针)的方法来判断。另外,因为题目中给的Lis

jan_deng的博客 308

LeetCode OJ 399. Evaluate Division

LeetCode OJ 399. Evaluate DivisionDescriptionEquations are given in the format A / B = k, where A and B are variables represented as strings, and k is a real number (floating point number). Given some

jan_deng的博客 300

LeetCode OJ 405. Convert a Number to Hexadecimal

LeetCode OJ 405. Convert a Number to HexadecimalDescriptionGiven an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method is used.Note: All letters in

jan_deng的博客 294

LeetCode 热题 100-普通数组

因为nums[i]应该在的位置索引是nums[i]-1,所以如果nums[nums[i]-1](按索引找出来的值)不是nums[i],那就换位置。lambda是匿名函数,lambda表达式本身产生一个函数对象,可以赋值给变量,传给 函数,立即调用。找半天错误,发现不该和intervals前一个区间做对比,应该和list1的最后一个区间做对比。我原本的想法是创建一个新列表,把轮转后的数字放在新列表中,然后把新列表赋值给nums。列表原地排序,排序依据是每个元素(一个区间)的第 0 个值,即左端点。

m0_74153997的博客 168
上一篇: LeetCode OJ 50. Pow(x, n)
下一篇: LeetCode OJ 160. Intersection of Two Linked Lists
jan_deng
博客等级 码龄11年 1粉丝 26原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值