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);
}
相关推荐
m0_629494732 小时前
LeetCode 热题 100-----26.环形链表 II
数据结构·算法·leetcode·链表
壹号用户2 小时前
用队列实现栈
数据结构·算法
做人求其滴2 小时前
面试经典 150 题 380 274
c++·算法·面试·职场和发展·力扣
daad7772 小时前
记一组无人机IMU传感器数据
算法
计算机安禾2 小时前
【c++面向对象编程】第42篇:模板特化与偏特化:为特定类型定制实现
开发语言·c++·算法
浩浩测试一下3 小时前
汇编 标志位寄存器 (逆向分析 )
c语言·汇编·逆向·windows编程·标志寄存器
小O的算法实验室3 小时前
2026年KBS,流形感知强化学习差分进化算法+不规则3D无人机路径规划,深度解析+性能实测
算法·智能算法·智能算法改进
玖釉-3 小时前
C++ 中的循环语句详解:while、do...while、for、嵌套循环与循环控制
开发语言·c++·算法
不做无法实现的梦~3 小时前
运动控制系统复习一览-----常考题目总结版本
算法
小短腿的代码世界3 小时前
信号路由风暴:Qt算法交易系统的高频信号分发架构
qt·算法·架构