当前位置: 首页 > 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);
}
http://www.lryc.cn/news/161852.html

相关文章:

  • 利用transform和border 创造简易图标,以适应uniapp中多字体大小情况下的符号问题
  • C/C++指针函数与函数指针
  • 30天入门Python(基础篇)——第1天:为什么选择Python
  • 智慧公厕破解公共厕所管理的“孤岛现象”
  • excel中删除重复项
  • 2023-9-8 求组合数(三)
  • 01 - Apache Seatunnel 源码调试
  • UVA-12325 宝箱 题解答案代码 算法竞赛入门经典第二版
  • 烟感报警器单片机方案开发,解决方案
  • 【JavaEE】_CSS引入方式与选择器
  • 【8】shader写入类中
  • Servlet注册迭代史
  • 合创汽车V09纵享商务丝滑?预售价32万元起,正式宣布大规模生产
  • 49. 视频热度问题
  • 【力扣练习题】加一
  • Linux--I/O复用之select
  • 数据结构大作业 成绩分析c语言程序设计
  • Consul学习笔记之-初识Consul
  • python实现读取并显示图片的两种方法
  • Spring Boot 整合 MyBatis
  • 2023高教社杯数学建模A题B题C题D题E题思路模型 国赛建模思路分享
  • 手机木马远程控制复现
  • linux 安装Docker
  • Java中的值传递与引用传递 含面试题
  • SQL中CONVERT()函数用法详解
  • 借助各大模型的优点生成原创视频(真人人声)Plus
  • 技能大赛物联网赛项参赛软件建设方案
  • 蓝桥杯官网练习题(凯撒加密)
  • JavaScript 数组中常用的方法
  • YOLOV7改进-添加基于注意力机制的目标检测头(DYHEAD)