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);
}
相关推荐
zyl76510 小时前
FlashAttention 详解:从 GPU 硬件、普通 Attention 到分块计算与 Online Softmax
算法
爱读书的小胖14 小时前
LeetCode 122:买卖股票的最佳时机 II(C)解题分析
算法
结局无敌14 小时前
React Native for OpenHarmony 实战:三方库 react-native-modal-dialog 的鸿蒙化适配指南
深度学习·算法·react native·react.js·harmonyos
OPEN-F14 小时前
ROS2系列教程:上位机建图与导航(原理综合)
算法·机器人
weixin_3077791314 小时前
C++代码实现MATLAB中的fitrtree函数功能
开发语言·c++·算法·matlab
爱读书的小胖14 小时前
Codewars:Rot13(C)解题分析
c语言·开发语言
tomwang032214 小时前
路径优化建模、优化算法分类及工程化小结
算法·数学建模·分类
小羊没烦恼!14 小时前
Ajax与JSON的一些总结
android·java·前端·数据库·mysql·算法·okhttp
2501_9269783315 小时前
人机通信协议--人和机器之间的交互想象
人工智能·经验分享·算法·ai写作
行者全栈架构师15 小时前
【鸿蒙心迹】互动卡片全链路——把核心功能放到桌面(FormKit 实战)(HarmonyOS 7.x)
前端·算法·架构