刷题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;
            if(root->left==NULL && root->right==NULL) return;
            TreeNode *p,* cur = root;

            stack<TreeNode*> st;
            if(root->right !=NULL){
                st.push(root->right);
            }
            if(root->left !=NULL){
                st.push(root->left);
            }

            while(! st.empty()){
                p = st.top();
                st.pop();
                cur->left = NULL;
                cur->right = p;
                cur = cur->right;

                if(cur->right !=NULL){
                    st.push(cur->right);
                }
                if(cur->left !=NULL){
                    st.push(cur->left);
                }
            }

            return;
        }
};

性能:

Runtime: 12 ms, faster than 27.18% of C++ online submissions for Flatten Binary Tree to Linked List.
Memory Usage: 11.6 MB, less than 8.33% of C++ online submissions for Flatten Binary Tree to Linked List.

三、优化措施

此处的“原地”,理解起来不能使用栈的。在这种情况下,将右子树作为左子树的最右下节点的右子树左子树变为右子树即可。

class Solution{
    public:
        void flatten(TreeNode* root){
            if(root==NULL) return;
            if(root->left !=NULL){
                TreeNode* pre = root->left;
                while(pre->right !=NULL){
                    pre = pre->right;
                }
                pre->right = root->right;
                root->right = root->left;
                root->left = NULL;
            }
            flatten(root->right);
            return;
        }
};

性能如下:

Runtime: 4 ms, faster than 95.35% of C++ online submissions for Flatten Binary Tree to Linked List.
Memory Usage: 11.3 MB, less than 8.33% of C++ online submissions for Flatten Binary Tree to Linked List.

这个性能还一般,用morris方法(线索化二叉树),空间复杂度可以到O(1)。

原文地址:https://www.cnblogs.com/siweihz/p/12267743.html

时间: 2024-09-29 16:14:20

刷题114. Flatten Binary Tree to Linked List的相关文章

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

!!!!!!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 ======== 思路: 将一个二叉树 就地 压成"链表"的结构;

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 OJ 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 click to show hints. Subscribe to see which companies asked this question 解答 先序遍历同时把节点都堆到左边,因为先序先处理左子树,所以这

8.7 114 Flatten Binary Tree to Linked List

Question: 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 Method: put root.left to its right, and connect root.right to its original left.right(if root.le

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

114. Flatten Binary Tree to Linked List (Stack, Tree; DFS)

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 法I:堆栈 struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), r

[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

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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 (二