LeetCode:114. Flatten Binary Tree to Linked List(固定二叉树为链表)

本文详细介绍了如何将二叉树转换为链表,提供了Java和Python两种语言的实现方式,包括利用栈和递归的方法,附带源码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文章最前: 我是Octopus,这个名字来源于我的中文名--章鱼;我热爱编程、热爱算法、热爱开源。所有源码在我的个人github ;这博客是记录我学习的点点滴滴,如果您对 Python、Java、AI、算法有兴趣,可以关注我的动态,一起学习,共同进步。

相关文章:

  1. LeetCode:55. Jump Game(跳远比赛)
  2. Leetcode:300. Longest Increasing Subsequence(最大增长序列)
  3. LeetCode:560. Subarray Sum Equals K(找出数组中连续子串和等于k)

文章目录:

题目描述:

java实现方式1:(利用栈的形式)

python实现方式1:

 java实现方式2:递归形式

python实现方式2:递归方式

源码github地址:https://github.com/zhangyu345293721/leetcode


题目描述:

给定一个二叉树,将它展开为链表。

例如,给定二叉树;

    1
   / \
  2   5
 / \   \
3   4   6

The flattened tree should look like:

1
 \
  2
   \
    3
     \
      4
       \
        5
         \
          6

java实现方式1:(利用栈的形式)

   /**
     * 将二叉树改为链表
     *
     * @root 根节点
     */
    public void flatten(TreeNode root) {
        if (root == null) {
            return;
        }
        Deque<TreeNode> stack = new LinkedList<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            TreeNode currentNode = stack.pop();
            if (currentNode.right != null) {
                stack.push(currentNode.right);
            }
            if (currentNode.left != null) {
                stack.push(currentNode.left);
            }
            if (!stack.isEmpty()) {
                currentNode.right = stack.peek();
            }
            // 左子树要为空
            currentNode.left = null;
        }
    }

时间复杂度:O(n)

空间复杂度:O(1)


python实现方式1:

def flatten(self, root: TreeNode) -> None:
        '''
            转换二叉树
        Args:
            root: 根节点
        Returns:
            只有有节点的二叉树
        '''
        if root == None:
            return
        stack = []
        stack.append(root)
        while len(stack) > 0:
            current_node = stack.pop()
            if current_node.right:
                stack.append(current_node.right)
            if current_node.left:
                stack.append(current_node.left)
            if len(stack) > 0:
                current_node.right = stack[-1]
            current_node.left = None

时间复杂度:O(n)

空间复杂度:O(1)


 java实现方式2:递归形式

   /**
     * 将二叉树改为链表
     *
     * @root 根节点
     */
    public void flatten2(TreeNode root) {
        flattenForList(root, null);
    }

    /**
     * 将二叉树遍历成链表
     *
     * @param root    根节点
     * @param current 当前节点
     * @return 节点
     */
    private TreeNode flattenForList(TreeNode root, TreeNode current) {
        if (root == null) {
            return current;
        }
        current = flattenForList(root.right, current);
        current = flattenForList(root.left, current);
        root.left = null;
        root.right = current;
        return root;
    }

时间复杂度:O(n)

空间复杂度:O(n)


python实现方式2:递归方式

def flatten_for_list(self, root: TreeNode, current_node) -> TreeNode:
        '''
            循环递归树
        Args:
            root: 根节点
            current_node: 当前节点
        Returns:
            root
        '''
        if not root:
            return current_node
        current_node = self.flatten_for_list(root.right, current_node)
        current_node = self.flatten_for_list(root.left, current_node)
        root.left = None
        root.right = current_node
        return root

def flatten2(self, root: TreeNode) -> None:
        '''
            转换二叉树
        Args:
            root: 根节点
        Returns:
            只有有节点的二叉树
        '''
        self.flatten_for_list(root, None)

时间复杂度:O(n)

空间复杂度:O(n)


源码github地址:

https://github.com/zhangyu345293721/leetcode

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值