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), right(NULL) {}
};

class Solution {
public:
    void flatten(TreeNode *root) {
        if(!root) return;
        TreeNode* flatRoot = new TreeNode(root->val);
        TreeNode* flatNode = flatRoot;
        TreeNode* current;
        stack<TreeNode*> tree_stack;

        //栈是先入后出,所以反序放入栈,即先右儿子,再左儿子
        if(root->right)
        {
            tree_stack.push(root->right);
        }

        if(root->left)
        {
            tree_stack.push(root->left);
        }
        while(!tree_stack.empty())
        {
            current = tree_stack.top();
            tree_stack.pop();

            flatNode->right = new TreeNode(current->val);
            flatNode = flatNode->right;

            //将出栈元素视为根节点,再次将它的右儿子、左儿子放入栈
            if(current->right)
            {
                tree_stack.push(current->right);
            }

            if(current->left)
            {
                tree_stack.push(current->left);
            }
        }

        *root = *flatRoot;
    }
};

法II:递归,前序遍历

class Solution {
public:
    void flatten(TreeNode *root) {
        if(!root) return;
        TreeNode* newRoot = new TreeNode(root->val);
        preOrderTraverse(root, newRoot);
        *root = *newRoot;
    }
    TreeNode* preOrderTraverse(TreeNode *root, TreeNode * newRoot)
    {
        if(root->left)
        {
            newRoot->right = new TreeNode(root->left->val);
            newRoot = preOrderTraverse(root->left, newRoot->right);
        }
        if(root->right)
        {
            newRoot->right = new TreeNode(root->right->val);
            newRoot = preOrderTraverse(root->right, newRoot->right);
        }
        return newRoot;
    }
};
时间: 2024-10-15 22:39:10

114. Flatten Binary Tree to Linked List (Stack, Tree; DFS)的相关文章

刷题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;

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 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

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 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 (二