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

拼搏现实的明天。 2022-04-03 09:38 241阅读 0赞

文章最前: 我是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. 1
  2. / \
  3. 2 5
  4. / \ \
  5. 3 4 6

The flattened tree should look like:

  1. 1
  2. \
  3. 2
  4. \
  5. 3
  6. \
  7. 4
  8. \
  9. 5
  10. \
  11. 6

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

  1. /**
  2. * 将二叉树改为链表
  3. *
  4. * @root 根节点
  5. */
  6. public void flatten(TreeNode root) {
  7. if (root == null) {
  8. return;
  9. }
  10. Deque<TreeNode> stack = new LinkedList<>();
  11. stack.push(root);
  12. while (!stack.isEmpty()) {
  13. TreeNode currentNode = stack.pop();
  14. if (currentNode.right != null) {
  15. stack.push(currentNode.right);
  16. }
  17. if (currentNode.left != null) {
  18. stack.push(currentNode.left);
  19. }
  20. if (!stack.isEmpty()) {
  21. currentNode.right = stack.peek();
  22. }
  23. // 左子树要为空
  24. currentNode.left = null;
  25. }
  26. }

时间复杂度:O(n)

空间复杂度:O(1)


python实现方式1:

  1. def flatten(self, root: TreeNode) -> None:
  2. '''
  3. 转换二叉树
  4. Args:
  5. root: 根节点
  6. Returns:
  7. 只有有节点的二叉树
  8. '''
  9. if root == None:
  10. return
  11. stack = []
  12. stack.append(root)
  13. while len(stack) > 0:
  14. current_node = stack.pop()
  15. if current_node.right:
  16. stack.append(current_node.right)
  17. if current_node.left:
  18. stack.append(current_node.left)
  19. if len(stack) > 0:
  20. current_node.right = stack[-1]
  21. current_node.left = None

时间复杂度:O(n)

空间复杂度:O(1)


java实现方式2:递归形式

  1. /**
  2. * 将二叉树改为链表
  3. *
  4. * @root 根节点
  5. */
  6. public void flatten2(TreeNode root) {
  7. flattenForList(root, null);
  8. }
  9. /**
  10. * 将二叉树遍历成链表
  11. *
  12. * @param root 根节点
  13. * @param current 当前节点
  14. * @return 节点
  15. */
  16. private TreeNode flattenForList(TreeNode root, TreeNode current) {
  17. if (root == null) {
  18. return current;
  19. }
  20. current = flattenForList(root.right, current);
  21. current = flattenForList(root.left, current);
  22. root.left = null;
  23. root.right = current;
  24. return root;
  25. }

时间复杂度:O(n)

空间复杂度:O(n)


python实现方式2:递归方式

  1. def flatten_for_list(self, root: TreeNode, current_node) -> TreeNode:
  2. '''
  3. 循环递归树
  4. Args:
  5. root: 根节点
  6. current_node: 当前节点
  7. Returns:
  8. root
  9. '''
  10. if not root:
  11. return current_node
  12. current_node = self.flatten_for_list(root.right, current_node)
  13. current_node = self.flatten_for_list(root.left, current_node)
  14. root.left = None
  15. root.right = current_node
  16. return root
  17. def flatten2(self, root: TreeNode) -> None:
  18. '''
  19. 转换二叉树
  20. Args:
  21. root: 根节点
  22. Returns:
  23. 只有有节点的二叉树
  24. '''
  25. self.flatten_for_list(root, None)

时间复杂度:O(n)

空间复杂度:O(n)


源码github地址:

https://github.com/zhangyu345293721/leetcode

发表评论

表情:
评论列表 (有 0 条评论,241人围观)

还没有评论,来说两句吧...

相关阅读