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:
c 复制代码
/**
 * 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);
}
相关推荐
持力行1 小时前
从C struct到C++中的class
c语言·c++
:-)3 小时前
算法-归并排序
java·开发语言·数据结构·算法·排序算法
2501_914245936 小时前
C语言设计模式详解:从理论到实践的完整指南
c语言·开发语言·设计模式
Jerry7 小时前
LeetCode 101. 对称二叉树
算法
可编程芯片开发8 小时前
基于MPPT最大功率跟踪的离网光伏发电系统Simulink建模与仿真
算法
AI科技星8 小时前
线性算子不是空间映射函数,是全域双螺旋场之间拉伸、旋转、耦合、坍缩的跨空间标准化变换载体《全域数学vs传统数学:人类文明进阶200讲》第80讲
线性代数·算法·矩阵·数据挖掘·回归·乖乖数学·全域数学
米罗篮8 小时前
矩阵快速幂 (Exponentiation By Squaring Applied To Matrices)
c++·线性代数·算法·矩阵
2501_914245938 小时前
C语言与硬件交互:从GPIO到中断的嵌入式编程实践
c语言·单片机·交互
dream_home84078 小时前
图像算法模型NPU适配与算法服务实战指南
人工智能·python·算法·npu 图像服务
大鱼>9 小时前
多宠物家庭智能管理平台:云端架构与多设备协同实战
python·算法·iot·宠物