当前位置:网站首页>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;
}
};

边栏推荐
- cartographer_ fast_ correlative_ scan_ matcher_ 2D branch and bound rough matching
- MySQL database-01 database overview
- Security problems in wireless networks and modern solutions
- Consul服务注册与发现
- 无线网络存在的安全问题及现代化解决方案
- Henkel database custom operator '~~‘
- 数据存储:MySQL之InnoDB与MyISAM的区别
- data = self._ data_ queue. get(timeout=timeout)
- 劣币驱逐良币的思考
- Leetcode114. 二叉树展开为链表
猜你喜欢

MySQL database-01 database overview

pytorch(网络模型)

Bingc (inheritance)

适配器模式

Supplementary course on basic knowledge of IM development (II): how to design a server-side storage architecture for a large number of image files?

基于SDN的DDoS攻击缓解

Leetcode114. Expand binary tree into linked list

Introduction to alluxio

There are applications related to web network request API in MATLAB (under update)
![[C language] deep analysis of data storage in memory](/img/2e/ff0b5326d796b9436f4a10c10cfe22.png)
[C language] deep analysis of data storage in memory
随机推荐
睛天霹雳的消息
慢慢学JVM之缓存行和伪共享
Command line interface of alluxio
【ARM】讯为rk3568开发板buildroot添加桌面应用
Leetcode114. 二叉树展开为链表
cartographer_ optimization_ problem_ 2d
Ribbon负载均衡服务调用
Use jedis to monitor redis stream to realize message queue function
SDN based DDoS attack mitigation
无线网络存在的安全问题及现代化解决方案
ZigBee learning in simple terms Lecture 1
使用Jedis監聽Redis Stream 實現消息隊列功能
数据存储:MySQL之InnoDB与MyISAM的区别
Something about MariaDB
机器学习 05:非线性支持向量机
[activity recommendation] cloud native, industrial Internet, low code, Web3, metauniverse... Which is the architecture hot spot in 2022
A new explanation of tcp/ip five layer protocol model
【ARM】在NUC977上搭建基于boa的嵌入式web服务器
使用Jenkins执行TestNg+Selenium+Jsoup自动化测试和生成ExtentReport测试报告
ZigBee explain in simple terms lesson 2 hardware related and IO operation