• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
LilyLiya
博客园    首页    新随笔    联系   管理    订阅  订阅
Invert Binary Tree
翻转二叉树(BFS, DFS,队列)

refer to : https://www.algoexpert.io/questions/Invert%20Binary%20Tree

1. problem statement.

swap every left node in the tree for its corresponding right node. 

2. 递归法(深度优先遍历)

O(n) time: we are traversing every single node

O(d) space: 需要存放 O(d) 个函数调用(d是树的深度)

 1 import java.util.*;
 2 
 3 class Program {
 4   //O(n) time | O(d) space
 5   public static void invertBinaryTree(BinaryTree tree) {
 6         if(tree ==null){
 7             return;
 8         }
 9         swapHelper(tree);
10         invertBinaryTree(tree.left);
11         invertBinaryTree(tree.right);
12         
13   }
14     
15     public static void swapHelper(BinaryTree tree){
16         BinaryTree left = tree.left;
17         tree.left = tree.right;
18         tree.right = left;
19     }
20 
21   static class BinaryTree {
22     public int value;
23     public BinaryTree left;
24     public BinaryTree right;
25 
26     public BinaryTree(int value) {
27       this.value = value;
28     }
29   }
30 }
 1 def invertBinaryTree(tree):
 3     if tree is None:
 4         return
 5     swapHelper(tree)
 6     invertBinaryTree(tree.left)
 7     invertBinaryTree(tree.right)
 8     
 9 
10 def swapHelper(tree):
11     tree.left, tree.right = tree.right, tree.left
12 
13 
14 # This is the class of the input binary tree.
15 class BinaryTree:
16     def __init__(self, value):
17         self.value = value
18         self.left = None
19         self.right = None

 

3.迭代法(广度优先遍历, 队列)

O(n) time 

O(n) space , 假设到了最后一层,我们把最后一层的所有孩子结点都加进队列中,for balanced tree, n/2 leaf nodes

  1. initialize a queue
  2. traverse level by level, root node-->next level
  3. traverse the tree by using BFS manually
  4. pop one node one time, get curr node
  5. swap the curr.left and curr.right
  6. add curr.left and curr.right into the queue
  7. repeat 4-6 until queue is empty.

import java.util.*;

class Program {
    //O(n) time | O(n) space
  public static void invertBinaryTree(BinaryTree tree) {
        ArrayDeque<BinaryTree> queue = new ArrayDeque<BinaryTree>();
        //addLast() method to insert at end 
        queue.addLast(tree);
        while(queue.size() > 0){
            //removes the first element of the Deque and returns the same
            BinaryTree curr = queue.pollFirst();
            swapHelper(curr);
            if(curr.left != null){
                queue.addLast(curr.left);
            }
            if(curr.right != null){
                queue.addLast(curr.right);
            }
        }
  }
    
    public static void swapHelper(BinaryTree tree){
        BinaryTree left = tree.left;
        tree.left = tree.right;
        tree.right = left;
    }

  static class BinaryTree {
    public int value;
    public BinaryTree left;
    public BinaryTree right;

    public BinaryTree(int value) {
      this.value = value;
    }
  }
}
def invertBinaryTree(tree):
    queue = [tree]
    while len(queue):
        curr = queue.pop(0)
        if curr is None:
            continue
        swapHelper(curr)
        queue.append(curr.left)
        queue.append(curr.right)

def swapHelper(tree):
    tree.left, tree.right = tree.right, tree.left
# This is the class of the input binary tree.
class BinaryTree:
    def __init__(self, value):
        self.value = value
        self.left = None
        self.right = None

 

posted on 2021-01-07 12:25  LilyLiya  阅读(71)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3