当前位置:网站首页>423-二叉树(110. 平衡二叉树、257. 二叉树的所有路径、100. 相同的树、404. 左叶子之和)
423-二叉树(110. 平衡二叉树、257. 二叉树的所有路径、100. 相同的树、404. 左叶子之和)
2022-06-26 05:42:00 【liufeng2023】
110. 平衡二叉树
class Solution {
int getHeight(TreeNode* node)
{
if(node == nullptr) return 0;
int left_height = getHeight(node->left);
if (left_height == -1) return -1;
int right_height = getHeight(node->right);
if (right_height == -1) return -1;
int result;
if (abs(left_height - right_height) > 1)
{
result = -1;
}
else
{
result = 1 + std::max(left_height, right_height);
}
return result;
}
public:
bool isBalanced(TreeNode* root) {
return getHeight(root) == -1 ? false : true;
}
};
257. 二叉树的所有路径
1、递归
class Solution {
public:
void traversal(TreeNode* cur, vector<int>& path, vector<string>& res)
{
path.push_back(cur->val);
if (cur->left == nullptr && cur->right == nullptr)
{
string s_path;
for (int i = 0; i < path.size() - 1; i++)
{
s_path += to_string(path[i]);
s_path += "->";
}
s_path += to_string(path[path.size() - 1]);
res.push_back(s_path);
return;
}
if (cur->left)
{
traversal(cur->left, path, res);
path.pop_back();
}
if (cur->right)
{
traversal(cur->right, path, res);
path.pop_back();
}
}
public:
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> res;
vector<int> path;
if (root == nullptr) return res;
traversal(root, path, res);
return res;
}
};
2、迭代法
class Solution {
public:
vector<string> binaryTreePaths(TreeNode* root) {
stack<TreeNode*> tree_st;
stack<string> path_st;
vector<string> res;
if (root == nullptr) return res;
tree_st.push(root);
path_st.push(to_string(root->val));
while (!tree_st.empty())
{
TreeNode* node = tree_st; tree_st.pop();
string path = path_st.top(); path_st.pop();
if (node->left == nullptr && node->right == nullptr)
{
result.push_back(path);
}
if (node->right)
{
tree_st.push(node->right);
path_st.push(path + "->" + to_string(node->right->val));
}
if (node->left)
{
tree_st.push(node->left);
path_st.push(path + "->" + to_string(node->left->val));
}
}
return res;
}
};
100. 相同的树
class Solution {
public:
bool compare(TreeNode* tree1, TreeNode* tree2)
{
if (tree1 == nullptr && tree2 != nullptr) return false;
else if (tree1 != nullptr && tree2 == nullptr) return false;
else if (tree1 == nullptr && tree2 == nullptr) return true;
else if (tree1->val != tree2->val) return false;
bool compare_left = compare(tree1->left, tree2->left);
bool compare_right = compare(tree1->right, tree2->right);
bool is_same = compare_left && compare_right;
return is_same;
}
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
return compare(p, q);
}
};
404. 左叶子之和
class Solution {
public:
int sumOfLeftLeaves(TreeNode* root) {
queue<TreeNode*> que;
int res = 0;
if (root) que.push(root);
while (!que.empty())
{
int size = que.size();
for (int i = 0; i < size; i++)
{
TreeNode* node = que.front();
que.pop();
if (node->left != nullptr && node->left->left == nullptr && node->left->right == nullptr)
{
res += node->left->val;
}
if (node->left)
{
que.push(node->left);
}
if (node->right) que.push(node->right);
}
}
return res;
}
};
边栏推荐
- data = self._ data_ queue. get(timeout=timeout)
- cross entropy loss = log softmax + nll loss
- 无线网络存在的安全问题及现代化解决方案
- cartographer_ fast_ correlative_ scan_ matcher_ 2D branch and bound rough matching
- 力扣 875. 爱吃香蕉的珂珂
- The model defined (modified) in pytoch loads some required pre training model parameters and freezes them
- Gd32f3x0 official PWM drive has a small positive bandwidth (inaccurate timing)
- How to ensure the efficiency and real-time of pushing large-scale group messages in mobile IM?
- Fedora alicloud source
- Henkel database custom operator '~~‘
猜你喜欢
How does P2P technology reduce the bandwidth of live video by 75%?
【ARM】在NUC977上搭建基于boa的嵌入式web服务器
cartographer_ fast_ correlative_ scan_ matcher_ 2D branch and bound rough matching
Posting - don't get lost in the ocean of Technology
Fedora alicloud source
The wechat team disclosed that the wechat interface is stuck with a super bug "15..." The context of
转帖——不要迷失在技术的海洋中
【 langage c】 stockage des données d'analyse approfondie en mémoire
Wechat team sharing: technical decryption behind wechat's 100 million daily real-time audio and video chats
pytorch(网络模型训练)
随机推荐
10 set
Replacing domestic image sources in openwrt for soft routing (take Alibaba cloud as an example)
Pytorch中自己所定义(修改)的模型加载所需部分预训练模型参数并冻结
Introduction to GUI programming to game practice (II)
The most refined language interprets the event dispatcher (also known as the event scheduler)
REUSE_ALV_GRID_DISPLAY 事件实现(DATA_CHANGED)
Old love letters
Pre-Sale Analysis
[upsampling method opencv interpolation]
A new journey
Daily production training report (16)
使用Jedis监听Redis Stream 实现消息队列功能
劣币驱逐良币的思考
[MySQL] MySQL million level data paging query method and its optimization
旧情书
Feelings of virtual project failure
pytorch(网络模型训练)
skimage. morphology. medial_ axis
Learn cache lines and pseudo sharing of JVM slowly
uniCloud云开发获取小程序用户openid