当前位置:网站首页>Binary tree - 226. Flip binary tree
Binary tree - 226. Flip binary tree
2022-07-26 00:04:00 【Xiao Zhao, who is working hard for millions of annual salary】
1 Title Description
Give you the root node of a binary tree root , Flip this binary tree , And return its root node .
2 Title Example


Example 3:
Input :root = []
Output :[]
3 Topic tips
The number of nodes in the tree ranges from [0, 100] Inside
-100 <= Node.val <= 100
4 Ideas
recursive :
This is a classic binary tree problem . obviously , We start at the root node , Recursively traverse the tree , And start flipping from the leaf node . If the node currently traversed root The two subtrees on the left and right have been turned over , Then we just need to exchange the positions of the two subtrees , Can be completed to root Is the flip of the entire subtree of the root node .
Complexity analysis
Time complexity :o(N), among N Is the number of binary tree nodes . We will traverse every node in the binary tree , For each node , We exchange its two subtrees in constant time .
· Spatial complexity :O(N). The space used is determined by the depth of the recursive stack , It is equal to the height of the current node in the binary tree . On average , The height of a binary tree has a logarithmic relationship with the number of nodes , namely O(log N). And in the worst case , Trees form chains , The space complexity is O(N).
iteration :
Recursive implementation is the way of depth first traversal , So the corresponding is breadth first traversal .
Breadth first traversal requires additional data structures – queue , To store the temporarily traversed elements .
Depth first traversal is characterized by a rod inserted to the end , No, go back and continue ; The characteristic of breadth first traversal is sweeping at all levels .
therefore , We need to put the root node in the queue first , Then iterate over the elements in the queue .
Swap the positions of the left and right subtrees of the current element , then :
Judge whether the left subtree is empty , If it is not empty, put it into the queue to judge whether its right subtree is empty , If it is not empty, it will be put into the queue
Time complexity : Similarly, each node needs to be queued / Outgoing queue — Time , So it is o(n)
Spatial complexity : In the worst case, all leaf nodes will be included , The leaf nodes of a complete binary tree are n/2 individual , So time complexity is 0(n)
5 My answer
recursive :
class Solution {
public TreeNode invertTree(TreeNode root) {
if (root == null) {
return null;
}
TreeNode left = invertTree(root.left);
TreeNode right = invertTree(root.right);
root.left = right;
root.right = left;
return root;
}
}
iteration :
class Solution {
public TreeNode invertTree(TreeNode root) {
if(root==null) {
return null;
}
// Put the nodes in the binary tree into the queue layer by layer , Then iterate over the elements in the queue
LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(root);
while(!queue.isEmpty()) {
// Take a node from the queue each time , And exchange the left and right subtrees of this node
TreeNode tmp = queue.poll();
TreeNode left = tmp.left;
tmp.left = tmp.right;
tmp.right = left;
// If the left subtree of the current node is not empty , Then put it into the queue for subsequent processing
if(tmp.left!=null) {
queue.add(tmp.left);
}
// If the right subtree of the current node is not empty , Then put it into the queue for subsequent processing
if(tmp.right!=null) {
queue.add(tmp.right);
}
}
// Return the processed root node
return root;
}
}
边栏推荐
- Are you still using your browser's own bookmarks? This bookmark plugin is awesome
- 结对编程实践心得
- Firewall command simple operation
- Unexpected dubug tricks
- 回溯——77. 组合
- 本轮牛市还能持续多久?|疑问解答 2021-05-11
- [learning notes] unreal 4 engine introduction (III)
- SQLZOO——Nobel Quiz
- Bubble sort idea and Implementation
- Lua script Wireshark plug-in to resolve third-party private protocols
猜你喜欢

MySQL的DDL、DML和DQL的基本语法

Fixed and alternate sequential execution of modes

抽丝剥茧C语言(高阶)程序环境和预处理

Leetcode107-二叉树的层序遍历II详解

Sort fake contacts

老旧笔记本电脑变服务器(笔记本电脑+内网穿透)

NVIDIA可编程推理加速器TensorRT学习笔记(三)——加速推理

The mobile version of Duoyu security browser will add new functions to make users browse more personalized

Are you still using your browser's own bookmarks? This bookmark plugin is awesome
![[learning notes] unreal 4 engine introduction (IV)](/img/30/4defa3cbd785d43adb405c71d16406.png)
[learning notes] unreal 4 engine introduction (IV)
随机推荐
浅识 OWASP
二叉树——257. 二叉树的所有路径
牛市还将继续,拿好手里的币 2021-05-08
Several ways of writing strings in reverse order
Getting started with Servlet
Pyqt5 rapid development and actual combat.pdf sharing
Exercise (3) create a list set (both ArrayList and LinkedList)
C language implementation of three chess
Solve the problem of rapid index bar extrusion
多御安全浏览器手机版将增加新功能,使用户浏览更个性化
栈与队列——347. 前 K 个高频元素
最近随感,关于牛市和DeFi 2021-05-17
The GUI interface of yolov3 (simple, image detection)
Weight file and pre training file of yolov3
【ManageEngine】ServiceDesk Plus荣获2022安全样板工程数据安全奖
Shardingsphere data slicing
07_ue4进阶_发射火球扣mp值和攻击扣血机制
@The underlying principle of Autowired annotation
Ten threats to open API ecosystem
The items of listview will be displayed completely after expansion