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);
}

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

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

游戏发行平台都有什么服务和功能?

游戏发行平台通常提供一系列服务和功能&#xff0c;以帮助游戏开发商将游戏推向市场&#xff0c;并为玩家提供游戏。以下是一些常见的游戏发行平台服务和功能&#xff1a; 1、游戏发布 发行平台允许游戏开发商将游戏上传到平台上&#xff0c;以供玩家下载和安装。 2、游戏销售…

Redis7--基础篇1(概述,安装、卸载及配置)

1. Redis概述 1.1 什么是Redis Redis&#xff1a;REmote Dictionary Server&#xff08;远程字典服务器&#xff09; Remote Dictionary Server(远程字典服务)是完全开源的&#xff0c;使用ANSIC语言编写遵守BSD协议&#xff0c;是一个高性能的Key-Value数据库提供了丰富的数…

Python 操作 CSV

使用过 CSV 文件都知道&#xff1a;如果我们的电脑中装了 WPS 或 Microsoft Office 的话&#xff0c;.csv 文件默认是被 Excel 打开的&#xff0c;那么什么是 CSV 文件&#xff1f;CSV 文件与 Excel 文件有什么区别&#xff1f;如何通过 Python 来操作 CSV 文件呢&#xff1f;带…

mfc 浮动窗口

参考 MFC模拟360悬浮窗加速球窗口

Codeforces Round 895 (Div. 3) A ~ F

Dashboard - Codeforces Round 895 (Div. 3) - Codeforces A 问多少次能使a 和 b相等&#xff0c;就是abs(a - b) / 2除c向上取整&#xff0c;也就是abs(a - b)除2c向上取整。 #include<bits/stdc.h> #define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); #de…

卡尔曼滤波公式推导(总结)

假设 小车在t时刻的初始状态可以用Pt&#xff08;当前位置&#xff09;&#xff0c;Vt&#xff08;当前速度&#xff09;&#xff0c;Ut表示加速度&#xff1a; 预测&#xff1a; 利用上一个时刻的旧状态和系统的动量模型&#xff08;如加速度&#xff0c;速度等&#xff09;…

MySQL(六)

集群 单节点数据库 优点&#xff1a;适合数据量小的网站&#xff0c;如企业网站。 缺点&#xff1a;单个数据库无法满足日益增长的读写请求&#xff1b; 为什么要使用集群 高可用性&#xff1a;站点高可用&#xff0c;冗余站点&#xff1b;服务高可用&#xff0c;冗余服务…

分享一个基于SpringBoot+Vue的房屋在线装修预约系统源码

&#x1f495;&#x1f495;作者&#xff1a;计算机源码社 &#x1f495;&#x1f495;个人简介&#xff1a;本人七年开发经验&#xff0c;擅长Java、Python、PHP、.NET、微信小程序、爬虫、大数据等&#xff0c;大家有这一块的问题可以一起交流&#xff01; &#x1f495;&…

C++学习笔记(重载、类)

C 1、函数重载2、类2.1、类的方法和属性2.2、类的方法的定义2.3、构造器和析构器2.4、基类与子类2.5、类的public、protected、private继承2.6、类的方法的重载2.7、子类方法的覆盖2.8、继承中的构造函数和析构函数 1、函数重载 函数重载大概可以理解为&#xff0c;定义两个名…

如何保持 SSH 会话不中断?

哈喽大家好&#xff0c;我是咸鱼 不知道小伙伴们有没有遇到过下面的情况&#xff1a; 使用终端&#xff08;XShell、secureCRT 或 MobaXterm 等&#xff09;登录 Linux 服务器之后如果有一段时间没有进行交互&#xff0c;SSH 会话就会断开 如果正在执行一些非后台命令&#…

Navicat连接mysql8.0:提示无法加载身份验证插件“caching_sha2_password”

Navicat连接mysql时&#xff0c;提示&#xff1a;Unable to load authentication plugin ‘caching_sha2_password‘. 原因&#xff1a;mysql 8.0 默认使用 caching_sha2_password 身份验证机制。 D:\MySQL8.0\install\bin>mysql -uroot -p123456789 #登录 mysql: [War…

单元测试界的高富帅,Pytest框架 (三) 用例标记和测试执行篇

pytest用例标记和测试执行篇 上一篇文章入门篇咱们介绍了pytest的前后置方法和fixture机制&#xff0c;这个章节主要给大家介绍pytest中的标记机制和用例执行的方法。pytest可以通过标记将数据传入于测试函数中&#xff0c;也可以通过标记中对执行的用例做筛选&#xff0c;接下…