LeetCode 114. Flatten Binary Tree to Linked List 动态演示

把二叉树先序遍历,变成一个链表,链表的next指针用right代替

用递归的办法先序遍历,递归函数要返回子树变成链表之后的最后一个元素

class Solution {
public:
    void helper(TreeNode* cur, TreeNode*& tail){
        //a(tail)
        //lk("root",tail)
        //a(cur)
        //lk("root",cur)
        //dsp
        tail=cur;
        TreeNode* right=cur->right;
        //a(right)
        //lk("root",right)
        if(cur->left){
            TreeNode *leftTail=NULL;
            helper(cur->left, leftTail);
            cur->right=cur->left;
            cur->left=NULL;
            tail=leftTail;
            //dsp
        }

        if(right){
            TreeNode *rightTail=NULL;
            helper(right, rightTail);
            tail->right=right;
            tail=rightTail;
            //dsp
        }
    }

    void flatten(TreeNode* root) {
        if(!root)
            return;
        //ahd(root)

        TreeNode *tail=NULL;
        helper(root, tail);
    }
};

程序运行动态演示:http://simpledsp.com/FS/Html/lc114.html

原文地址:https://www.cnblogs.com/leetcoder/p/11330354.html

时间: 2024-10-07 11:00:49

LeetCode 114. Flatten Binary Tree to Linked List 动态演示的相关文章

leetcode 114. Flatten Binary Tree to Linked List (Python版)

题目: Given a binary tree, flatten it to a linked list in-place. 算法思路: 其实该题目就是二叉树前序遍历的变形 我代码沿用leetcode 144. Binary Tree Preorder Traversal 代码: class Solution(object):     def preorderTraversal(self, root):         """         :type root: Tree

Java for LeetCode 114 Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6解题思路:试图通过排序后new TreeNode是无法通过的,这道题的意思是把现有的树进行剪枝操作.JAVA实现如下: static public void flatten(TreeNode root) { wh

[LeetCode] 114. Flatten Binary Tree to Linked List Java

题目: Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 题意及分析:给出一棵树,要求给出这棵树的先序遍历组成的链表,但是还是用树表示.首先找到根节点左子节点的最右子节点,然后将根节点的右子树移到该点的右节点上:再将根节点的左子节点移到根节点的右子节点上,并将根

[leetcode]114. Flatten Binary Tree to Linked List由二叉树构建链表

/* 先序遍历构建链表,重新构建树 */ LinkedList<Integer> list = new LinkedList<>(); public void flatten(TreeNode root) { preOrder(root); TreeNode res = root; list.poll(); while (!list.isEmpty()) { res.right = new TreeNode(list.poll()); res.left = null; res =

114 Flatten Binary Tree to Linked List [Python]

114 Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. 将二叉树展开成链表 [[]D:\dataStructure\Leetcode\114.png] (D:\dataStructure\Leetcode\114.png "114") 思路:将根节点与左子树相连,再与右子树相连.递归地在每个节点的左右孩子节点上,分别进行这样的操作. 代码 clas

[LeetCode 题解]: Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 Hints: If you notice carefully in the flattened tree, each node's right child points to the next node of a

刷题114. Flatten Binary Tree to Linked List

一.题目说明 题目114. Flatten Binary Tree to Linked List,将一个二叉树"原地"压缩为"链表"形态的二叉树.难度为Medium! 二.我的解答 这个题目如果允许使用栈的话Easy,先序遍历二叉树,右子树入栈,左子树入栈.当栈不空的时候,将栈顶元素放到右子树即可. class Solution{ public: void flatten(TreeNode* root){ //先根遍历 if(root==NULL) return;

【LeetCode】Flatten Binary Tree to Linked List

题目:Flatten Binary Tree to Linked List <span style="font-size:18px;">/**LeetCode Flatten Binary Tree to Linked List * 题意:给定一个二叉树,将其转变为一个相当于单链表的结构,观察可知该结构即:每一个节点左儿子为空,右儿子指向自己先序遍历时的下一个节点 * 思路:有观察可得,应对其进行先序遍历,得到正确的序列连接起来 * Definition for binar

leetcode dfs Flatten Binary Tree to Linked List

Flatten Binary Tree to Linked List Total Accepted: 25034 Total Submissions: 88947My Submissions Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 click to