当前位置:网站首页>leetcode:226. Flip binary tree
leetcode:226. Flip binary tree
2022-06-29 14:03:00 【uncle_ ll】
226. Flip binary tree
source : Power button (LeetCode)
link : https://leetcode.cn/problems/invert-binary-tree/
Give you the root node of a binary tree root , Flip this binary tree , And return its root node .
Example 1:
Input :root = [4,2,7,1,3,6,9]
Output :[4,7,2,9,6,3,1]
Example 2:

Input :root = [2,1,3]
Output :[2,3,1]
Example 3:
Input :root = []
Output :[]
Tips :
- The number of nodes in the tree ranges from [0, 100] Inside
- -100 <= Node.val <= 100
solution
- recursive : Start at the root node , Swap two child nodes , Then recursively process the two child nodes
- bfs loop : Use a queue to store the nodes of each tier , Then each time from the queue header pop Let's get a node , Swap the child nodes of this node , If the child node is not empty , Add child nodes to the queue ;
Code implementation
recursive
python Realization
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def invertTree(self, root: TreeNode) -> TreeNode:
# Recursion end condition
if not root or (not root.left and not root.right):
return root
# Process this node
root.left, root.right = root.right, root.left
# Recursive next node
self.invertTree(root.left)
self.invertTree(root.right)
return root
c++ Realization
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if (root == nullptr || (root->left == nullptr && root->right == nullptr))
return root;
TreeNode* tmp = root->left;
root->left = root->right;
root->right = tmp;
if (root->left != nullptr)
invertTree(root->left);
if (root->right != nullptr)
invertTree(root->right);
return root;
}
};
Complexity analysis
- Time complexity : O ( N ) O(N) O(N)
- Spatial complexity : O ( N ) O(N) 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 ( l o g N ) O(log N) O(logN) . And in the worst case , Trees form chains , The space complexity is O ( N ) O(N) O(N).
BFS loop
python Realization
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def invertTree(self, root: TreeNode) -> TreeNode:
if not root or (not root.left and not root.right):
return root
stack = [root]
while stack:
node = stack.pop(0)
node.left, node.right = node.right, node.left
if node.left:
stack.append(node.left)
if node.right:
stack.append(node.right)
return root
c++ Realization
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if (root == nullptr || (root->left == nullptr && root->right == nullptr))
return root;
queue<TreeNode*> qs;
qs.push(root);
TreeNode *tmp;
while(qs.size() > 0) {
tmp = qs.front();
qs.pop();
TreeNode * tmp2 = tmp->left;
tmp->left = tmp->right;
tmp->right = tmp2;
if (tmp->left != nullptr)
invertTree(tmp->left);
if (tmp->right != nullptr)
invertTree(tmp->right);
}
return root;
}
};
Complexity analysis
- Time complexity : O ( N ) O(N) O(N)
- Spatial complexity : O ( N ) O(N) O(N)
边栏推荐
- golang代码规范整理
- 灵感收集·创意写作软件评测:Flomo、Obsidian Memo、Napkin、FlowUs
- 【云驻共创】通过Rust语言计算加速技术突破图片识别性能瓶颈
- ES6 array method
- Interpretation of RESNET source code in mmdet +ghost module
- Applet Wechat: un nouveau réseau exclusif de microgroupes de développement de Cloud
- 如何优雅的写 Controller 层代码?
- Shell——文本处理命令
- 超 Nice 的表格响应式布局小技巧
- 微信小程序:图片秒加水印制作生成
猜你喜欢

中康控股开启招股:拟募资净额3.95亿港元,预计7月12日上市

微信小程序:装B神器P图修改微信流量主小程序源码下载趣味恶搞图制作免服务器域名

【毕业季·进击的技术er】1076万毕业生,史上最难就业季?卷又卷不过,躺又躺不平,敢问路在何方?

留给比亚迪的时间还有三年

Sixty years of deep learning

关于MongoDB报错:connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb

硬件开发笔记(八): 硬件开发基本流程,制作一个USB转RS232的模块(七):创建基础DIP元器件(晶振)封装并关联原理图元器件

“死掉”的诺基亚,一年躺赚1500亿

Mondo rescue creates an image file (which is conducive to image damage recovery)

How to make dapper support dateonly type
随机推荐
微信小程序:云开发表白墙微信小程序源码下载免服务器和域名支持流量主收益
微信小程序:全新独家云开发微群人脉
Goby full port scan
疯狂的数字藏品,下一个造富神话?
在线文本过滤小于指定长度工具
微信小程序:图片秒加水印制作生成
台式机主板上保护cpu的盖子安装和拆卸
微信小程序:(更新)云开发微群人脉
golang代码规范整理
koa2+better-sqlite3实现增删改查
纳人才,谋发展 | 人大金仓喜获“最佳雇主校招案例奖”
System. Currenttimemillis() and system Nanotime() which is faster? Most people get the wrong answer!
Koa2+better-sqlite3 to add, delete, change and query
Interpretation of RESNET source code in mmdet +ghost module
硬件开发笔记(八): 硬件开发基本流程,制作一个USB转RS232的模块(七):创建基础DIP元器件(晶振)封装并关联原理图元器件
Go unit testing introductory practice
微信小程序:大红喜庆版UI猜灯谜又叫猜字谜
分布式唯一 ID 生成方案浅谈
Grep exact match
【云驻共创】通过Rust语言计算加速技术突破图片识别性能瓶颈