当前位置: 首页 > news >正文

LeetCode //C - 114. Flatten Binary Tree to Linked List

114. Flatten Binary Tree to Linked List

Given the root of a binary tree, flatten the tree into a “linked list”:

  • The “linked list” should use the same TreeNode class where the right child pointer points to the next node in the list and the left child pointer is always null.
  • The “linked list” should be in the same order as a pre-order traversal of the binary tree.
     

Example 1:

在这里插入图片描述

Input: root = [1,2,5,3,4,null,6]
Output: [1,null,2,null,3,null,4,null,5,null,6]

Example 2:

Input: root = []
Output: []

Example 3:

Input: root = [0]
Output: [0]

Constraints:

  • The number of nodes in the tree is in the range [0, 2000].
  • -100 <= Node.val <= 100

From: LeetCode
Link: 114. Flatten Binary Tree to Linked List


Solution:

Ideas:
  1. Modified Pre-Order Traversal: Traditional pre-order traversal visits a node in the order: root, left subtree, and then right subtree. The modification here is that we’re doing it in a slightly different order: we first flatten the right subtree, then the left subtree, and finally process the current root.

  2. Global prev Variable: This variable keeps track of the last node that we’ve visited. When we visit a new node, we’ll be linking this node to the prev node using the right pointer.

  3. Flattening Process:

  • When we visit a node:
    • We recursively flatten its right subtree.
    • We recursively flatten its left subtree.
    • We then update the current node’s right pointer to point to the prev node. This effectively appends the previously processed list to the current node.
    • We set the current node’s left pointer to NULL (because we want the linked list to use the right pointers).
    • Finally, we update the prev node to be the current node, as this node will be the previous node for the next node we process.
  1. Resetting the prev Variable: Before starting the flattening process for a tree (or a subtree), we reset the prev variable to NULL. This ensures that the last node in the flattened list will correctly point to NULL instead of some node from a previous test case or a previous run.

  2. Auxiliary Recursive Function: We’ve split the logic into two functions:

  • flatten_recursive handles the actual recursive flattening logic.
  • flatten is the main function that resets the prev variable and then calls the recursive function to perform the flattening.
Code:
/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     struct TreeNode *left;*     struct TreeNode *right;* };*/
struct TreeNode* prev = NULL;void flatten_recursive(struct TreeNode* root) {if (!root) return;flatten_recursive(root->right);flatten_recursive(root->left);root->right = prev;root->left = NULL;prev = root;
}void flatten(struct TreeNode* root) {prev = NULL;  // Reset the prev variableflatten_recursive(root);
}

相关文章:

LeetCode //C - 114. Flatten Binary Tree to Linked List

114. Flatten Binary Tree to Linked List Given the root of a binary tree, flatten the tree into a “linked list”: The “linked list” should use the same TreeNode class where the right child pointer points to the next node in the list and the left child …...

利用transform和border 创造简易图标,以适应uniapp中多字体大小情况下的符号问题

heml: <text class"icon-check"></text> css: .icon-check {border: 2px solid black;border-left: 0;border-top: 0;height: 12px;width: 6px;transform-origin: center;transform: rotate(45deg);} 实际上就是声明一个带边框的div 将其中相邻的两边去…...

C/C++指针函数与函数指针

一、指针函数 指针函数&#xff1a;本质为一个函数&#xff0c;返回值为指针指针函数&#xff1a;如果一个函数的返回值是指针类型&#xff0c;则称为指针函数用指针作为函数的返回值的好处&#xff1a;可以从被调函数向主函数返回大量的数据&#xff0c;常用于返回结构体指针。…...

30天入门Python(基础篇)——第1天:为什么选择Python

文章目录 专栏导读作者有话说为什么学习Python原因1(总体得说)原因2(就业说) Python的由来(来自百度百科)Python的版本 专栏导读 &#x1f525;&#x1f525;本文已收录于《30天学习Python从入门到精通》 &#x1f251;&#x1f251;本专栏专门针对于零基础和需要重新复习巩固…...

智慧公厕破解公共厕所管理的“孤岛现象”

在现代社会中&#xff0c;公共厕所是城市管理中的一项重要任务。然而&#xff0c;经常会出现公厕管理的“孤岛现象”&#xff0c;即每个公厕都是独立运作&#xff0c;缺乏统一的管理和监控机制。针对这一问题&#xff0c;智慧公厕的出现为解决公共厕所管理难题带来了新的方案。…...

excel中删除重复项

数据如图&#xff1a; 要删除姓名这一列的重复项&#xff0c;操作&#xff1a; (1)选中姓名这一列(2)点击“数据”(3)点击“删除重复项" 这是excel会自动检测出还有别的关联列 直接默认&#xff0c;点击删除重复项...弹出下面的界面 因为我们只要删除“姓名”列的重复值&…...

2023-9-8 求组合数(三)

题目链接&#xff1a;求组合数 III #include <iostream> #include <algorithm>using namespace std;typedef long long LL;int p;int qmi(int a, int k) {int res 1;while(k){if(k & 1) res (LL) res * a % p;k >> 1;a (LL) a * a % p;}return res; }…...

01 - Apache Seatunnel 源码调试

1.下载源码 https://github.com/apache/seatunnel.git2.编译 mvn clean package -pl seatunnel-dist -am -Dmaven.test.skiptrue3. 下载驱动 sh bin/install-plugin.sh 4.测试类 选择 seatunnel-examples ├── seatunnel-engine-examples ├── seatunnel-flink-connecto…...

UVA-12325 宝箱 题解答案代码 算法竞赛入门经典第二版

GitHub - jzplp/aoapc-UVA-Answer: 算法竞赛入门经典 例题和习题答案 刘汝佳 第二版 根据书上的方法来做&#xff0c;是比较简单的题目。关键在于知道等体积时的枚举法。不过数据大小可能很大&#xff0c;虽然输入可以用int处理&#xff0c;但是 体积*价值 后&#xff0c;需要l…...

烟感报警器单片机方案开发,解决方案

烟感报警器也叫做烟雾报警器。烟感报警器适用于火灾发生时有大量烟雾&#xff0c;而正常情况下无烟的场所。例如写字楼、医院、学校、博物馆等场所。烟感报警器一般安装于所需要保护或探测区域的天花板上&#xff0c;因火灾中烟雾比空气轻&#xff0c;更容易向上飘散&#xff0…...

【JavaEE】_CSS引入方式与选择器

目录 1. 基本语法格式 2. 引入方式 2.1 内部样式 2.2 内联样式 2.3 外部样式 3. 基础选择器 3.1 标签选择器 3.2 类选择器 3.3 ID选择器 4. 复合选择器 4.1 后代选择器 4.2 子选择器 4.3 并集选择器 4.4 伪类选择器 1. 基本语法格式 选择器若干属性声明 2. 引入…...

【8】shader写入类中

上一篇将 vao vbo写入类中进行封装&#xff0c;本篇将shader进行封装。 Shader shader("res/shaders/Basic.shader");shader.Bind(); shader.SetUniform4f("u_Color", 0.2f, 0.3f, 0.8f, 1.0f);shader.h #pragma once#include <string> #include &l…...

Servlet注册迭代史

Servlet注册迭代史 1、第一代&#xff0c;xml注册 <web-app><display-name>Archetype Created Web Application</display-name><!-- 定义一个Servlet --><servlet><!-- Servlet的名称&#xff0c;用于在配置中引用 --><servlet-name&…...

合创汽车V09纵享商务丝滑?预售价32万元起,正式宣布大规模生产

合创汽车正式宣布&#xff0c;旗下新款车型V09已于9月10日开始大规模生产&#xff0c;并预计将于10月13日正式上市。V09作为中大型纯电动MPV的代表之一&#xff0c;备受瞩目。该车型是广汽新能源和蔚来汽车共同成立的广汽蔚来改为广汽集团和珠江投管共同投资的高端品牌——合创…...

49. 视频热度问题

文章目录 实现一题目来源 谨以此笔记献给浪费掉的两个小时。 此题存在多处疑点和表达错误的地方&#xff0c;如果你看到了这篇文章&#xff0c;劝你跳过该题。 该题对提升HSQL编写能力以及思维逻辑能力毫无帮助。 实现一 with info as (-- 将数据与 video_info 关联&#x…...

【力扣练习题】加一

package sim;import java.math.BigDecimal; import java.util.Arrays;public class Add1 {/*给定一个由 整数 组成的 非空 数组所表示的非负整数&#xff0c;在该数的基础上加一。最高位数字存放在数组的首位&#xff0c; 数组中每个元素只存储单个数字。你可以假设除了整数 0 …...

Linux--I/O复用之select

目录 一&#xff1a;概念 二&#xff1a;使用 三&#xff1a;参数介绍&#xff1a; 1.ndfs&#xff1a; 2.fd_set类型&#xff1a; 3.readfds&#xff1a; 4.writefds&#xff1a; 5.exceptfds&#xff1a; 6.timeout&#xff1a; 7.返回值&#xff1a; 四&#xff1…...

数据结构大作业 成绩分析c语言程序设计

界面加载 界面展示 成绩输入 求平均成绩 升序排列 降序排列 名字排序 按名字搜索 按ID搜索 每门课成绩分析 成绩单展示 -...

Consul学习笔记之-初识Consul

文章目录 1. What is consul?2. Consul能干什么3. Consul的架构3.1 概念 4. Consul VS Eureka4.1 CAP4.2 对比 1. What is consul? 根据官方文档的定义&#xff1a; HashiCorp Consul is a service networking solution that enables teams to manage secure network connec…...

python实现读取并显示图片的两种方法

前言 嗨喽&#xff0c;大家好呀~这里是爱看美女的茜茜呐 在 python 中除了用 opencv&#xff0c;也可以用 matplotlib 和 PIL 这两个库操作图片。 本人偏爱 matpoltlib&#xff0c;因为它的语法更像 matlab。 &#x1f447; &#x1f447; &#x1f447; 更多精彩机密、教程&…...

从一次安全事件复盘:我们是如何通过配置Windows审计策略和事件查看器,发现并阻断虚拟机异常登录的

虚拟化环境安全审计实战&#xff1a;从异常登录告警到精准防御 那天凌晨3点15分&#xff0c;安全运营中心的告警铃声突然响起。监控大屏上&#xff0c;一台核心业务虚拟机的登录事件触发了我们的阈值告警——这个时间段本不该有任何运维操作。当我调出事件查看器里那条4672特殊…...

实战指南:基于Cursor与快马平台,从零搭建一个可用的商品管理后台

今天想和大家分享一个实战项目——用Cursor和InsCode(快马)平台从零搭建商品管理后台的全过程。这个项目麻雀虽小五脏俱全&#xff0c;包含了前后端完整链路&#xff0c;特别适合想练手全栈开发的朋友。 项目架构设计 整个系统采用前后端分离模式。后端用Spring Boot搭建RESTfu…...

springboot同城二手物品交易配送系统的设计与实现

目录需求分析与系统设计核心功能模块开发安全与性能优化测试与部署方案项目技术支持源码获取详细视频演示 &#xff1a;文章底部获取博主联系方式&#xff01;同行可合作需求分析与系统设计 进行详细的需求调研&#xff0c;明确用户角色&#xff08;买家、卖家、管理员&#x…...

OmenSuperHub:惠普游戏本性能控制终极指南 - 开源替代方案全面解析

OmenSuperHub&#xff1a;惠普游戏本性能控制终极指南 - 开源替代方案全面解析 【免费下载链接】OmenSuperHub 项目地址: https://gitcode.com/gh_mirrors/om/OmenSuperHub 还在为惠普Omen Gaming Hub的臃肿体积和隐私担忧而烦恼吗&#xff1f;OmenSuperHub为你提供了一…...

华硕笔记本性能困境突破:G-Helper工具的全方位优化方案

华硕笔记本性能困境突破&#xff1a;G-Helper工具的全方位优化方案 【免费下载链接】g-helper Lightweight Armoury Crate alternative for Asus laptops. Control tool for ROG Zephyrus G14, G15, G16, M16, Flow X13, Flow X16, TUF, Strix, Scar and other models 项目地…...

1771-OZL处理器模块

1771-OZL 处理器模块 — 产品特点1771-OZL 是1771系列的PLC处理器模块&#xff0c;用于工业自动化系统的逻辑运算与过程控制。适用于PLC-5标准机架控制系统支持数字量输入/输出及模拟量接口内置高速逻辑运算功能可执行顺序控制和定时/计数功能支持程序存储与在线修改高可靠性设…...

Unity游戏开发:如何用UniTask实现可撤销的异步流程(附完整代码)

Unity游戏开发&#xff1a;UniTask实现可撤销异步流程的工程实践 在游戏开发中&#xff0c;异步操作的管理一直是让开发者头疼的问题。想象这样一个场景&#xff1a;玩家在教学关卡中反复尝试某个操作&#xff0c;需要随时回退到上一步&#xff1b;或者在剧情分支选择时&#…...

别再踩坑PX4Flow了!实测优象LC-302光流模块,手把手教你搞定PX4无人机室内悬停

无人机室内悬停实战指南&#xff1a;优象LC-302光流模块深度评测与PX4调参技巧 当无人机从开阔的室外飞入复杂的室内环境&#xff0c;GPS信号的突然消失往往让飞手们手忙脚乱。这时&#xff0c;一套可靠的光流定位系统就成了"空中救生绳"。本文将带您深入评测市面上主…...

7-Zip ZS:六种压缩算法如何彻底改变你的文件处理体验

7-Zip ZS&#xff1a;六种压缩算法如何彻底改变你的文件处理体验 【免费下载链接】7-Zip-zstd 7-Zip with support for Brotli, Fast-LZMA2, Lizard, LZ4, LZ5 and Zstandard 项目地址: https://gitcode.com/gh_mirrors/7z/7-Zip-zstd 在数字时代&#xff0c;文件压缩已…...

使用xrdp实现Windows远程桌面无缝连接WSL2中的Ubuntu24.04

1. 为什么需要远程桌面连接WSL2&#xff1f; 很多开发者习惯在Windows系统上使用WSL2运行Ubuntu进行开发工作&#xff0c;但默认情况下WSL2只提供命令行界面。虽然大多数开发任务可以通过命令行完成&#xff0c;但有些场景下图形界面会更方便&#xff1a; 运行需要GUI的应用程…...