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

Posted stAr_1

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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 = res.right;
        }
    }
    public void preOrder(TreeNode root)
    {
        if (root==null)
            return;
        list.offer(root.val);
        preOrder(root.left);
        preOrder(root.right);
    }

 

以上是关于[leetcode]114. Flatten Binary Tree to Linked List由二叉树构建链表的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 114. Flatten Binary Tree to Linked List

leetcode--114. Flatten Binary Tree to Linked List

LeetCode 114. Flatten Binary Tree to Linked List

Leetcode 114, Flatten Binary Tree to Linked List

LeetCode-114-Flatten Binary Tree to Linked List

LeetCode OJ 114. Flatten Binary Tree to Linked List