所有的LeetCode题解索引,可以看这篇文章——【算法和数据结构】LeetCode题解。
一、题目
二、解法
思路分析:思路比较简单,遍历所有节点然后判断该节点是否为左叶子节点,如果是,将其值累加。遍历算法采用【算法与数据结构】144、94、145LeetCode二叉树的前中后遍历(递归法、迭代法)文章中递归前序遍历算法,设置了一个左节点标志符,遍历左节点时,标识符为1,左右节点为空则为叶子节点。
程序如下:
class Solution {
public:
void traversal_preOrder(TreeNode* cur, int& sum, int left_flag) { // 前序遍历
if (cur == NULL) return;
if (!cur->left && !cur->right && left_flag) sum += cur->val; // 叶子节点且为左节点
traversal_preOrder(cur->left, sum, 1); // 左
traversal_preOrder(cur->right, sum, 0); // 右
}
// 遍历所有节点,判断每个节点的左节点是否为叶子结点,然后相加
int sumOfLeftLeaves(TreeNode* root) {
int result = 0;
traversal_preOrder(root, result, 0);
return result;
}
};
复杂度分析:
- 时间复杂度: O ( n ) O(n) O(n)。
- 空间复杂度: O ( n ) O(n) O(n)。
三、完整代码
# include<iostream>
# include<string>
# include<vector>
# include<queue>
using namespace std;
// 树节点定义
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};
class Solution {
public:
void traversal_preOrder(TreeNode* cur, int& sum, int left_flag) { // 前序遍历
if (cur == NULL) return;
if (!cur->left && !cur->right && left_flag) sum += cur->val; // 叶子节点且为左节点
traversal_preOrder(cur->left, sum, 1); // 左
traversal_preOrder(cur->right, sum, 0); // 右
}
// 遍历所有节点,判断每个节点的左节点是否为叶子结点,然后相加
int sumOfLeftLeaves(TreeNode* root) {
int result = 0;
traversal_preOrder(root, result, 0);
return result;
}
};
// 前序遍历迭代法创建二叉树,每次迭代将容器首元素弹出(弹出代码还可以再优化)
void Tree_Generator(vector<string>& t, TreeNode*& node) {
if (!t.size() || t[0] == "NULL") return; // 退出条件
else {
node = new TreeNode(stoi(t[0].c_str())); // 中
if (t.size()) {
t.assign(t.begin() + 1, t.end());
Tree_Generator(t, node->left); // 左
}
if (t.size()) {
t.assign(t.begin() + 1, t.end());
Tree_Generator(t, node->right); // 右
}
}
}
// 层序遍历
vector<vector<int>> levelOrder(TreeNode* root) {
queue<TreeNode*> que;
if (root != NULL) que.push(root);
vector<vector<int>> result;
while (!que.empty()) {
int size = que.size(); // size必须固定, que.size()是不断变化的
vector<int> vec;
for (int i = 0; i < size; ++i) {
TreeNode* node = que.front();
que.pop();
vec.push_back(node->val);
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
result.push_back(vec);
}
return result;
}
template<typename T>
void my_print(T& v, const string msg)
{
cout << msg << endl;
for (class T::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << ' ';
}
cout << endl;
}
template<class T1, class T2>
void my_print2(T1& v, const string str) {
cout << str << endl;
for (class T1::iterator vit = v.begin(); vit < v.end(); ++vit) {
for (class T2::iterator it = (*vit).begin(); it < (*vit).end(); ++it) {
cout << *it << ' ';
}
cout << endl;
}
}
int main()
{
vector<string> t = { "3", "9", "NULL", "NULL", "20", "15", "NULL", "NULL", "7", "NULL", "NULL" }; // 前序遍历
my_print(t, "目标树");
TreeNode* root = new TreeNode();
Tree_Generator(t, root);
vector<vector<int>> tree = levelOrder(root);
my_print2<vector<vector<int>>, vector<int>>(tree, "目标树:");
Solution s;
int result = s.sumOfLeftLeaves(root);
cout << "sumOfLeftLeaves: " << result << endl;
system("pause");
return 0;
}
end