LeetCode //C - 114. Flatten Binary Tree to Linked List

这篇具有很好参考价值的文章主要介绍了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:

LeetCode //C - 114. Flatten Binary Tree to Linked List,LeetCode,leetcode,c语言,算法

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:文章来源地址https://www.toymoban.com/news/detail-707204.html

  • 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 variable
    flatten_recursive(root);
}

到了这里,关于LeetCode //C - 114. Flatten Binary Tree to Linked List的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • LeetCode //C - 92. Reverse Linked List II

    Given the head of a singly linked list and two integers left and right where left = right, reverse the nodes of the list from position left to position right , and return the reversed list.   Example 1: Input: head = [1,2,3,4,5], left = 2, right = 4 Output: [1,4,3,2,5] Example 2: Input: head = [5], left = 1, right = 1 Output: [5] Constraints: The number of

    2024年02月11日
    浏览(29)
  • 【set】个人练习-Leetcode-817. Linked List Components

    题目链接:https://leetcode.cn/problems/linked-list-components/description/ 题目大意:给出一个 vectorint nums ,其中有一些数字。再给出一个链表的头指针 head ,链表内的元素各不相同。如果链表中有某一段(长度大于等于1)的元素都在 nums 中出现过,那么就算一个component,求链表中的co

    2024年02月13日
    浏览(30)
  • LeetCode //C - 328. Odd Even Linked List

    Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list. The first node is considered odd, and the second node is even, and so on. Note that the relative order inside both the even and odd groups should remain as it was in the input. You must solve the proble

    2024年01月25日
    浏览(30)
  • LeetCode //C - 124. Binary Tree Maximum Path Sum

    A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence at most once. Note that the path does not need to pass through the root. The path sum of a path is the sum of the node’s values in the path. Given the root of a binary tree, return the maximum

    2024年02月09日
    浏览(31)
  • LeetCode //C - 199. Binary Tree Right Side View

    Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.   Example 1: Input: root = [1,2,3,null,5,null,4] Output: [1,3,4] Example 2: Input: root = [1,null,3] Output: [1,3] Example 3: Input root = [] Output [] Constraints: The number of nodes in the tree is in t

    2024年01月20日
    浏览(28)
  • LeetCode 92. Reverse Linked List II【链表,头插法】中等

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2024年02月09日
    浏览(31)
  • 【LeetCode 算法】Linked List Cycle II 环形链表 II

    给定一个链表的头节点 head ,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。 如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从

    2024年02月14日
    浏览(32)
  • LeetCode //C - 1161. Maximum Level Sum of a Binary Tree

    Given the root of a binary tree, the level of its root is 1, the level of its children is 2, and so on. Return the smallest level x such that the sum of all the values of nodes at level x is maximal.   Example 1: Input: root = [1,7,0,7,-8,null,null] Output: 2 **Explanation: ** Level 1 sum = 1. Level 2 sum = 7 + 0 = 7. Level 3 sum = 7 + -8 = -1. So we return

    2024年01月23日
    浏览(39)
  • LeetCode 1019. Next Greater Node In Linked List【单调栈模板】中等

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2023年04月18日
    浏览(26)
  • LeetCode //C - 2130. Maximum Twin Sum of a Linked List

    In a linked list of size n, where n is even, the i t h i^{th} i t h node (0-indexed) of the linked list is known as the twin of the ( n − 1 − i ) t h (n-1-i)^{th} ( n − 1 − i ) t h node, if 0 = i = (n / 2) - 1. For example, if n = 4, then node 0 is the twin of node 3, and node 1 is the twin of node 2. These are the only nodes with twins for n = 4. Th

    2024年01月17日
    浏览(38)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包