ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Invert Binary Tree

2021-01-07 12:33:10  阅读:225  来源: 互联网

标签:Binary right tree Invert Tree BinaryTree value public left


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 

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

 

标签:Binary,right,tree,Invert,Tree,BinaryTree,value,public,left
来源: https://www.cnblogs.com/LilyLiya/p/14245773.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有