当前位置:网站首页>421-二叉树(226. 翻转二叉树、101. 对称二叉树、104.二叉树的最大深度、222.完全二叉树的节点个数)
421-二叉树(226. 翻转二叉树、101. 对称二叉树、104.二叉树的最大深度、222.完全二叉树的节点个数)
2022-06-26 05:42:00 【liufeng2023】
226. 翻转二叉树

1、递归法
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if (root == nullptr) return root;
swap(root->left, root->right);
invertTree(root->left);
invertTree(root->right);
return root;
}
};
2、迭代法
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if (root == nullptr) return nullptr;
stack<TreeNode*> st;
st.push(root);
while (!st.empty())
{
TreeNode* node = st.top();
st.pop();
swap(node->left, node->right);
if (node->right) st.push(node->right);
if (node->left) st.push(node->left);
}
return root;
}
};

101. 对称二叉树

1、递归法
class Solution {
public:
bool compare(TreeNode* left, TreeNode* right)
{
if (left == nullptr && right != nullptr) return false;
else if (left != nullptr && right == nullptr) return false;
else if (left == nullptr && right == nullptr) return true;
else if (left->val != right->val) return false;
else return compare(left->left, right->right) && compare(left->right, right->left);
}
public:
bool isSymmetric(TreeNode* root) {
if (root == nullptr) return true;
return compare(root->left, root->right);
}
};
2、迭代法
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if (root == NULL) return true;
queue<TreeNode*> que;
que.push(root->left); // 将左子树头结点加入队列
que.push(root->right); // 将右子树头结点加入队列
while (!que.empty()) {
// 接下来就要判断这两个树是否相互翻转
TreeNode* leftNode = que.front(); que.pop();
TreeNode* rightNode = que.front(); que.pop();
if (!leftNode && !rightNode) {
// 左节点为空、右节点为空,此时说明是对称的
continue;
}
// 左右一个节点不为空,或者都不为空但数值不相同,返回false
if ((!leftNode || !rightNode || (leftNode->val != rightNode->val))) {
return false;
}
que.push(leftNode->left); // 加入左节点左孩子
que.push(rightNode->right); // 加入右节点右孩子
que.push(leftNode->right); // 加入左节点右孩子
que.push(rightNode->left); // 加入右节点左孩子
}
return true;
}
};

104.二叉树的最大深度

1、递归法
class Solution {
public:
int get_depth(TreeNode* node)
{
if (node == nullptr) return 0;
int left_depth = get_depth(node->left);
int right_depth = get_depth(node->right);
int depth = 1 + std::max(left_depth, right_depth);
return depth;
}
public:
int maxDepth(TreeNode* root) {
return get_depth(root);
}
};
2、迭代法
class Solution {
public:
int maxDepth(TreeNode* root) {
queue<TreeNode*> que;
if (root) que.push(root);
int res = 0;
while (!que.empty())
{
int size = que.size();
res++;
for (int i = 0; i < size; i++)
{
TreeNode* node = que.front();
que.pop();
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
}
return res;
}
};

222.完全二叉树的节点个数

1、递归法
class Solution {
public:
int get_nodes(TreeNode* node)
{
if (node == nullptr) return 0;
int left_nodes = get_nodes(node->left);
int right_nodes = get_nodes(node->right);
int nodes = 1 + left_nodes + right_nodes;
return nodes;
}
public:
int countNodes(TreeNode* root) {
return get_nodes(root);
}
};
2、迭代法
class Solution {
public:
int countNodes(TreeNode* root) {
queue<TreeNode*> que;
if(root) que.push(root);
int res = 0;
while(!que.empty())
{
int size = que.size();
for(int i = 0; i < size; i++)
{
res++;
TreeNode* node = que.front();
que.pop();
if(node->left) que.push(node->left);
if(node->right) que.push(node->right);
}
}
return res;
}
};

边栏推荐
- Recursively traverse directory structure and tree presentation
- bingc(继承)
- uni-app吸顶固定样式
- 国务院发文,完善身份认证、电子印章等应用,加强数字政府建设
- 【ARM】讯为rk3568开发板buildroot添加桌面应用
- 小小面试题之GET和POST的区别
- Supplementary course on basic knowledge of IM development (II): how to design a server-side storage architecture for a large number of image files?
- one billion two hundred and twelve million three hundred and twelve thousand three hundred and twenty-one
- Secondary bootloader about boot28 Precautions for ASM application, 28035
- Could not get unknown property ‘*‘ for SigningConfig container of type org.gradle.api.internal
猜你喜欢

【ARM】讯为rk3568开发板buildroot添加桌面应用

MySQL数据库-01数据库概述

Mongodb image configuration method

Leetcode513.找出树的左下角的值

Sofa weekly | open source person - Yu Yu, QA this week, contributor this week

As promised: Mars, the mobile terminal IM network layer cross platform component library used by wechat, has been officially open source

Redis discovery bloom filter
![[activity recommendation] cloud native, industrial Internet, low code, Web3, metauniverse... Which is the architecture hot spot in 2022](/img/64/5b2aec7a26c64c104c86e200f83b2d.png)
[activity recommendation] cloud native, industrial Internet, low code, Web3, metauniverse... Which is the architecture hot spot in 2022

基于SDN的DDoS攻击缓解

Redis usage and memory optimization
随机推荐
使用Jenkins执行TestNg+Selenium+Jsoup自动化测试和生成ExtentReport测试报告
【C語言】深度剖析數據在內存中的存儲
机器学习 07:PCA 及其 sklearn 源码解读
【ARM】讯为rk3568开发板buildroot添加桌面应用
项目中止
Leetcode114. 二叉树展开为链表
Supplementary course on basic knowledge of IM development (II): how to design a server-side storage architecture for a large number of image files?
使用Jedis监听Redis Stream 实现消息队列功能
REUSE_ALV_GRID_DISPLAY 事件实现(DATA_CHANGED)
RIA想法
Talk 5 wireless communication
操作符的优先级、结合性、是否控制求值顺序【详解】
cartographer_ backend_ constraint
redis探索之布隆过滤器
Navicat如何将当前连接信息复用另一台电脑
Pytorch中自己所定义(修改)的模型加载所需部分预训练模型参数并冻结
Replacing domestic image sources in openwrt for soft routing (take Alibaba cloud as an example)
Introduction to lcm32f037 series of MCU chip for motor
C# 40. Byte[] to hexadecimal string
【PHP】PHP二维数组按照多个字段进行排序