2 Star 0 Fork 0

狐皮先生/nb.c

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
7_8.cpp 5.05 KB
一键复制 编辑 原始数据 按行查看 历史
狐皮先生 提交于 2023-07-08 23:32 . 7_8
#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) {}
};
int findBottomLeftValue(TreeNode* root) {
queue<TreeNode*>q;
int number = 0;
if (root)
{
q.push(root);
number = 1;
}
vector<vector<int>> vv;
while (!q.empty())
{
vector<int>v;
for (int i = 0; i < number; ++i)
{
TreeNode* front = q.front();
v.push_back(front->val);
if (front->left)
q.push(front->left);
if (front->right)
q.push(front->right);
q.pop();
}
vv.push_back(v);
number = q.size();
}
int row = vv.size();
return vv[row][0];
}
int main()
{
TreeNode* n1 = new TreeNode(1);
TreeNode* n2 = new TreeNode(2);
TreeNode* n3 = new TreeNode(3);
TreeNode* n4 = new TreeNode(4);
TreeNode* n5 = new TreeNode(5);
TreeNode* n6 = new TreeNode(6);
TreeNode* n7 = new TreeNode(7);
n1->left = n2;
n1->right = n3;
n2->left = n4;
n3->left = n5;
n3->right = n6;
n5->left = n6;
findBottomLeftValue(n1);
return 0;
}
/**
* Definition for a binary tree node.
* 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:
// vector<int> largestValues(TreeNode* root) {
// queue<TreeNode*> q;
// size_t levelSize = 0;
// if (root)
// {
// q.push(root);
// levelSize = 1;
// }
// vector<vector<int>> vv;
// while (!q.empty())
// {
// vector<int>v;
// for (int i = 0; i < levelSize; ++i)
// {
// TreeNode* front = q.front();
// v.push_back(front->val);
// if (front->left)
// q.push(front->left);
// if (front->right)
// q.push(front->right);
// q.pop();
// }
// vv.push_back(v);
// //当前层出完了,下一层也都进入,更改levelSize
// levelSize = q.size();
// }
// vector<int> ret;
// for (size_t i = 0; i < vv.size(); i++)
// {
// int max = vv[i][0];
// for (size_t j = 0; j < vv[i].size(); j++)
// {
// if (max < vv[i][j])
//// {
//// max = vv[i][j];
//// }
//// }
//// ret.push_back(max);
//// }
//// return ret;
//
//
//
//
// }
//};
////#include <iostream>
////#include <string>
////using namespace std;
////
////
////string replaceSpaces(string s, int length) {
//// string str(s, length);
//// string ret;
//// int sz = str.size();
//// for (auto& e : str)
//// {
//// if (e == ' ')
//// {
//// ret += "%20";
//// }
//// else
//// {
//// ret += e;
//// }
//// }
//// int diff = length - sz;
//// while (diff--)
// {
// ret += "%20";
// }
// return ret;
//}
//#include<vector>
//
//int main() {
//
// vector<int> a = { 1,2,3,4,5 };
//
// auto e=a.erase(a.begin());
//
//
// int n=0;
// //cin >> n;
// //while (n--)
// //{
// // getline(cin, str,'\n');
// // if (str.size() <= 10)
// // {
// // cout << str << endl;
// // }
// // else
// // {
// // size_t len = str.size();
// // len -= 2;
// // cout << str[0] << len << str[str.size() - 1] << endl;
// // }
// //}
// return 0;
//}
//// 64 位输出请用 printf("%lld")
////#include <stdio.h>
////#include<string.h>
////#include<stdio.h>
////char* f(char* str, char ch)
////{
//// char* it1 = str;
//// char* it2 = str;
//// while (*it2 != '\0')
//// {
//// while (*it2 == ch)
//// {
//// it2++;
//// }
//// *it1++ = *it2++;
//// }
//// return str;
////}
////int main() {
//// char a[10];
//// strcpy(a, "abcdcccd");
//// printf("%s", f(a, 'c'));
//// return 0;
////}
//////int main()
//////{
////// char p1[15] = "abcd", p2[] = "ABCD", str[50] = "xyz";
////// strcpy(str + 2, strcat(p1 + 2, p2 + 1));
////// printf("%s", str);
////// return 0;
//////}
////
//////int main()
//////{
//////
////// char a[] = "abcd";
////// char* p = a;
////// char ch = (*p)++;
////// printf("%c\n", ch);
////// printf("%s", a);
//////
////// //const char* str[3] = { "stra", "strb", "strc" };
////// //const char* p = str[0];
////// //int i = 0;
////// //while (i < 3)
////// //{
////// // printf("%s ", p++);
////// // i++;
////// //}
////// return 0;
//////}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mr-fox-skin/nb.c.git
git@gitee.com:mr-fox-skin/nb.c.git
mr-fox-skin
nb.c
nb.c
master

搜索帮助