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

news2024/9/22 7:26:25

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 variable
    flatten_recursive(root);
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/986992.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

在Android和iOS上设置手机ip详细教程

大家好&#xff01;今天我们将分享一个关于如何在Android和iOS设备上设置手机ip&#xff08;Layer 2 Tunneling Protocol&#xff09;的简易教程。如果你想要通过安全且可靠的方式连接到远程网络&#xff0c;那么跟着本文一起学习吧&#xff01;无需复杂操作&#xff0c;让我们…

KUKA机器人后台控制程序(SPS)介绍

KUKA机器人后台控制程序(SPS)介绍 KUKA机器人后台控制程序主要包括以下几部分: RC:运动控制、机器人轨迹规划 优先级1 I/O刷新:输入输出信号的控制 优先级1 SPS:用户可编辑的后台逻辑程序 优先级2 显示界面刷新:示教器显示画面的控制 优先级3 以上的程序需要12ms(固定…

企业架构LNMP学习笔记19

Nginx 第三方模块的使用&#xff1a; Nginx官方没有的功能&#xff0c;开源开发人员定制开发了一些功能&#xff0c;把代码公布出来&#xff0c;可以通过编译加载第三方模块的方式&#xff0c;使用新功能。 NGINX 3rd Party Modules | NGINX shell > tar xvf ngx-fancyinde…

CVE-2017-12149

春秋云镜 CVE-2017-12149 JBoss反序列化漏洞 靶标介绍 2017年8月30日&#xff0c;厂商Redhat发布了一个JBOSSAS 5.x 的反序列化远程代码执行漏洞通告。该漏洞位于JBoss的HttpInvoker组件中的 ReadOnlyAccessFilter 过滤器中&#xff0c;其doFilter方法在没有进行任何安全检查…

Win10怎么设置待机时间

我们在使用电脑的过程中&#xff0c;经常因为有事需要离开电脑&#xff0c;长时间不操作电脑就会进行待机睡眠状态&#xff0c;那么Win10怎么设置待机时间呢&#xff0c;下面小编就给大家详细介绍一下Win10设置待机时间的方法&#xff0c;大家感兴趣的话可以来看一看。 设置方…

新华社《中国扫描十年发展图鉴》:扫描全能王为3亿用户带去“掌心里的便利”

扫描设备从“两手搬”到“进口袋”的过程中经历了什么&#xff1f; 近日&#xff0c;新华社发布了《中国扫描十年发展图鉴》&#xff08;简称《图鉴》&#xff09;&#xff0c;对扫描设备、技术、应用领域的发展历史进行了深入盘点。《图鉴》显示&#xff0c;扫描一度是价格接…

MySQL——数据的删除以及MySQL中的约束

删除数据 删除表中的一行数据&#xff0c;也必须加上 WHERE条件&#xff0c;否则整列的数据都会被删除。删除语句&#xff1a; delete from 表名 where 条件; 他会将所有的符合条件的数据删除&#xff0c;如果不写条件&#xff0c;则表中的数据全部删除&#xff1a; 如果不添…

软件测试/测试开发丨学会与 AI 对话,高效提升学习效率

点此获取更多相关资料 简介 ChatGPT 的主要优点之一是它能够理解和响应自然语言输入。在日常生活中&#xff0c;沟通本来就是很重要的一门课程&#xff0c;沟通的过程中表达越清晰&#xff0c;给到的信息越多&#xff0c;那么沟通就越顺畅。 和 ChatGPT 沟通也是同样的道理&…

方向介绍:基于深度学习的轨迹预测

方向介绍&#xff1a;基于深度学习的轨迹预测 文章目录 方向介绍&#xff1a;基于深度学习的轨迹预测问题定义典型方法挑战未来展望参考 基于深度学习的轨迹预测是一种利用神经网络模型来预测移动物体的未来位置和运动状态的技术。这种技术在许多领域都有重要的应用&#xff0c…

Jetpack Compose 入门教程之Text

这个文本显示组件应该是我们最常用的组件,下面会非常细 归纳 实例 下面一一演示这些属性与控制逻辑 文本的展示 Text组件 所有构造方法都是text:String,要想用string.xml里面的字符串资源 得使用 stringResource方法,其相似方法如下/** Copyright 2019 The Android Open Sou…

如何挑选低值易耗品管理系统?优化企业管理效率与成本控制

在现代企业管理中&#xff0c;低值易耗品的管理是一个容易被忽视但却十分重要的环节。低值易耗品包括办公用品、耗材、工具等&#xff0c;它们虽然单价不高&#xff0c;但数量庞大且频繁使用&#xff0c;对企业的日常运营和成本控制有着重要影响。为了提高管理效率、降低成本&a…

Linux与shell命令行学习

文章目录 走进shell基本的bash shell命令2.1 遍历目录 cd2.2 查看文件和目录列表 ls2.3 创建文件 touch2.4 复制文件 cp2.5 自动补全 tab2.6 链接文件 ln2.7 文件重命名 mv2.8 删除文件 rm2.9 创建目录 mkdir2.10 删除目录 rmdir2.11 查看文件类型 file2.12 查看整个文件 cat、…

flume1.11.0安装部署

1、准备安装包apache-flume-1.11.0-bin.tar.gz&#xff1b; 上传&#xff1b; 2、安装flume-1.11.0&#xff1b; 解压&#xff1b; tar -zxvf apache-flume-1.11.0-bin.tar.gz -C /opt/server 进入conf目录&#xff0c;修改flume-env.sh&#xff0c;配置JAVA_HOME&#xff1b…

docker 生成镜像的几个问题

docker 生成镜像的几个问题 根据jdk8.tar.gz 打包Jdk8 镜像失败运行镜像报错差不多是网络ip错误,在网上说重启docker即可解决运行mysql5.7.25 镜像失败向daemon.json文件添加内容导致docker重启失败docker run 命令常用参数根据jdk8.tar.gz 打包Jdk8 镜像失败 首选做准备工作…

有向图和无向图的表示方式(邻接矩阵,邻接表)

目录 一.邻接矩阵 1.无向图​编辑 2.有向图 补充&#xff1a;网&#xff08;有权图&#xff09;的邻接矩阵表示法 二.邻接表 1.无向图 2.有向图 三.邻接矩阵与邻接表的关系 一.邻接矩阵 1.无向图 &#xff08;1&#xff09;对角线上是每一个顶点与自身之间的关系&…

智慧能源方案:TSINGSEE青犀AI算法中台在能源行业的应用

一、方案背景 互联网、物联网、人工智能等新一代信息技术引领新一轮产业革命&#xff0c;加快能源革命步伐。尤其是随着人工智能技术的不断发展&#xff0c;AI智能检测与识别技术在能源行业的应用也越来越广泛。与此同时&#xff0c;国家出台多项政策&#xff0c;将智慧能源纳…

【HTML5高级第三篇】drag拖拽、音频视频、defer/async属性、dialog应用

文章目录 一、拖拽事件1.1 拖拽事件1.2 案例&#xff1a;拖拽丢弃图片 二、音频和视频三、defer 与 async 属性3.1 概述3.2 示例一&#xff1a;3.3 示例二&#xff1a; 四、dialog 元素 一、拖拽事件 原生JavaScipt案例合集 JavaScript DOM基础 JavaScript 基础到高级 Canvas…

LabVIEW利用局部放电分析高压电气设备状态诊断

LabVIEW利用局部放电分析高压电气设备状态诊断 目前&#xff0c;高压电气设备状态的监控系统解决了早期故障检测的问题。局部放电起源于电力电气装置的绝缘。局部放电会导致绝缘层逐渐磨损和加速老化&#xff0c;因此可能导致绝缘完全击穿。因此&#xff0c;局部放电检测及其特…

gitLab(git)误提交命令

1.先使用下面命令查看一下分支上已提交的信息 git log 2.回退到之前的版本 git reset —hard 你要删除的提交哈希码&#xff08;一般是离这个命令最近的一串数字&#xff09; 3.覆盖掉远端的版本信息&#xff0c;使远端的仓库也回退到相应的版本 注意&#xff1a;切换到你提…

安装K8s基础环境软件(二)

所有节点执行 1、安装docker sudo yum install -y yum-utils sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.reposudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin systemctl…