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);
}
相关推荐
2601_949146536 小时前
C语言语音通知接口接入教程:如何使用C语言直接调用语音预警API
c语言·开发语言
你撅嘴真丑6 小时前
第九章-数字三角形
算法
uesowys6 小时前
Apache Spark算法开发指导-One-vs-Rest classifier
人工智能·算法·spark
ValhallaCoder6 小时前
hot100-二叉树I
数据结构·python·算法·二叉树
董董灿是个攻城狮6 小时前
AI 视觉连载1:像素
算法
智驱力人工智能7 小时前
小区高空抛物AI实时预警方案 筑牢社区头顶安全的实践 高空抛物检测 高空抛物监控安装教程 高空抛物误报率优化方案 高空抛物监控案例分享
人工智能·深度学习·opencv·算法·安全·yolo·边缘计算
孞㐑¥7 小时前
算法——BFS
开发语言·c++·经验分享·笔记·算法
月挽清风8 小时前
代码随想录第十五天
数据结构·算法·leetcode
XX風8 小时前
8.1 PFH&&FPFH
图像处理·算法
知南x8 小时前
【Ascend C系列课程(高级)】(1) 算子调试+调优
c语言·开发语言