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

边栏推荐
- [red team] what preparations should be made to join the red team?
- RIA ideas
- skimage. morphology. medial_ axis
- The wechat team disclosed that the wechat interface is stuck with a super bug "15..." The context of
- MySQL数据库-01数据库概述
- A new explanation of tcp/ip five layer protocol model
- Source code of findcontrol
- Red team scoring method statistics
- 自定义WebSerivce作为代理解决SilverLight跨域调用WebService问题
- When was the autowiredannotationbeanpostprocessor instantiated?
猜你喜欢

Could not get unknown property ‘*‘ for SigningConfig container of type org.gradle.api.internal

redis探索之布隆过滤器

There are applications related to web network request API in MATLAB (under update)

Learn cache lines and pseudo sharing of JVM slowly

操作符的优先级、结合性、是否控制求值顺序【详解】

Mongodb image configuration method

基于SDN的DDoS攻击缓解

MySQL数据库-01数据库概述

【C语言】深度剖析数据在内存中的存储
![[red team] what preparations should be made to join the red team?](/img/03/f246f18f8925167dbd5e9d63912faa.png)
[red team] what preparations should be made to join the red team?
随机推荐
慢慢学JVM之缓存行和伪共享
Supplementary course on basic knowledge of IM development (II): how to design a server-side storage architecture for a large number of image files?
cartographer_ local_ trajectory_ builder_ 2d
转帖——不要迷失在技术的海洋中
DOM文档
Daily production training report (16)
C# 39. Conversion between string type and byte[] type (actual measurement)
FindControl的源代码
C XX management system
Leetcode513. Find the value in the lower left corner of the tree
Thinking about bad money expelling good money
SDN based DDoS attack mitigation
写在父亲节前
uni-app吸顶固定样式
原型模式,咩咩乱叫
Sofa weekly | open source person - Yu Yu, QA this week, contributor this week
LeetCode_ Binary search tree_ Simple_ 108. convert an ordered array to a binary search tree
About XXX management system (version C)
RIA想法
[activity recommendation] cloud native, industrial Internet, low code, Web3, metauniverse... Which is the architecture hot spot in 2022