当前位置:网站首页>421- binary tree (226. reversed binary tree, 101. symmetric binary tree, 104. maximum depth of binary tree, 222. number of nodes of complete binary tree)
421- binary tree (226. reversed binary tree, 101. symmetric binary tree, 104. maximum depth of binary tree, 222. number of nodes of complete binary tree)
2022-06-26 05:44:00 【liufeng2023】
226. Flip binary tree

1、 Recursive method
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、 Iterative method
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. Symmetric binary tree

1、 Recursive method
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、 Iterative method
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if (root == NULL) return true;
queue<TreeNode*> que;
que.push(root->left); // Add the head node of the left subtree to the queue
que.push(root->right); // Add the head node of the right subtree to the queue
while (!que.empty()) {
// Next, we need to judge whether the two trees flip each other
TreeNode* leftNode = que.front(); que.pop();
TreeNode* rightNode = que.front(); que.pop();
if (!leftNode && !rightNode) {
// Left node is empty 、 The right node is empty , At this time, the description is symmetrical
continue;
}
// The left and right nodes are not empty , Or they are not empty, but the values are different , return false
if ((!leftNode || !rightNode || (leftNode->val != rightNode->val))) {
return false;
}
que.push(leftNode->left); // Join the left node and the left child
que.push(rightNode->right); // Join the right node and the right child
que.push(leftNode->right); // Join the left node and the right child
que.push(rightNode->left); // Join the right node and the left child
}
return true;
}
};

104. The maximum depth of a binary tree

1、 Recursive method
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、 Iterative method
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. The number of nodes in a complete binary tree

1、 Recursive method
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、 Iterative method
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;
}
};

边栏推荐
- There are applications related to web network request API in MATLAB (under update)
- 12 multithreading
- 421-二叉树(226. 翻转二叉树、101. 对称二叉树、104.二叉树的最大深度、222.完全二叉树的节点个数)
- A new journey
- 项目中止
- REUSE_ALV_GRID_DISPLAY 事件实现(DATA_CHANGED)
- AutowiredAnnotationBeanPostProcessor什么时候被实例化的?
- 力扣 875. 爱吃香蕉的珂珂
- Security problems in wireless networks and modern solutions
- 睛天霹雳的消息
猜你喜欢

pytorch(环境、tensorboard、transforms、torchvision、dataloader)

DOM document

423-二叉树(110. 平衡二叉树、257. 二叉树的所有路径、100. 相同的树、404. 左叶子之和)

pytorch(网络模型训练)

REUSE_ALV_GRID_DISPLAY 事件实现(DATA_CHANGED)

REUSE_ ALV_ GRID_ Display event implementation (data_changed)

【C语言】深度剖析数据在内存中的存储
![[upsampling method opencv interpolation]](/img/6b/5e8f3c2844f0cbbbf03022e0efd5f0.png)
[upsampling method opencv interpolation]

Henkel database custom operator '~~‘

Uni app ceiling fixed style
随机推荐
Lesson 4 serial port and clock
pytorch(网络模型)
Could not get unknown property ‘*‘ for SigningConfig container of type org.gradle.api.internal
Pytorch中自己所定义(修改)的模型加载所需部分预训练模型参数并冻结
MySQL database-01 database overview
[arm] build boa based embedded web server on nuc977
SQL query time period content
[arm] add desktop application for buildreoot of rk3568 development board
Ribbon负载均衡服务调用
Internship May 29, 2019
Positioning setting horizontal and vertical center (multiple methods)
慢慢学JVM之缓存行和伪共享
About XXX management system (version C)
Written before father's Day
Pytorch (network model)
基于SDN的DDoS攻击缓解
kolla-ansible部署openstack yoga版本
转帖——不要迷失在技术的海洋中
Henkel database custom operator '~~‘
[C language] deep analysis of data storage in memory