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

边栏推荐
- 力扣 875. 爱吃香蕉的珂珂
- LeetCode_ Binary search tree_ Simple_ 108. convert an ordered array to a binary search tree
- Gram 矩阵
- 【 langage c】 stockage des données d'analyse approfondie en mémoire
- 机器学习 07:PCA 及其 sklearn 源码解读
- Describe an experiment of Kali ARP in LAN
- 机器学习 05:非线性支持向量机
- 电机专用MCU芯片LCM32F037系列内容介绍
- [PHP] PHP two-dimensional array is sorted by multiple fields
- Introduction to GUI programming to game practice (I)
猜你喜欢

cartographer_ optimization_ problem_ 2d

Yunqi lab recommends experience scenarios this week, free cloud learning

How to ensure the efficiency and real-time of pushing large-scale group messages in mobile IM?
The wechat team disclosed that the wechat interface is stuck with a super bug "15..." The context of

Command line interface of alluxio

Fedora alicloud source

SOFA Weekly | 开源人—于雨、本周 QA、本周 Contributor

Learn cache lines and pseudo sharing of JVM slowly

As promised: Mars, the mobile terminal IM network layer cross platform component library used by wechat, has been officially open source
![[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
随机推荐
Mise en file d'attente des messages en utilisant jedis Listening redis stream
Owasp-top10 in 2021
Leetcode513.找出树的左下角的值
AutowiredAnnotationBeanPostProcessor什么时候被实例化的?
虚拟项目失败感想
How Navicat reuses the current connection information to another computer
Two step processing of string regular matching to get JSON list
[PHP] PHP two-dimensional array is sorted by multiple fields
When was the autowiredannotationbeanpostprocessor instantiated?
How does P2P technology reduce the bandwidth of live video by 75%?
Redis discovery bloom filter
redis探索之布隆过滤器
C# 40. Byte[] to hexadecimal string
【活动推荐】云原生、产业互联网、低代码、Web3、元宇宙……哪个是 2022 年架构热点?...
旧情书
写在父亲节前
【 langage c】 stockage des données d'analyse approfondie en mémoire
Source code of findcontrol
Introduction to lcm32f037 series of MCU chip for motor
Daily production training report (17)