当前位置: 首页 > news >正文

青岛高品质网站建设大数据查询个人信息

青岛高品质网站建设,大数据查询个人信息,it运维服务,2015年全球网站优秀设计师文章目录 一、题目二、解法三、完整代码 所有的LeetCode题解索引,可以看这篇文章——【算法和数据结构】LeetCode题解。 一、题目 二、解法 思路分析:本题通过计算根节点到叶子节点路径上节点的值之和,然后再对比目标值。利用文章【算法和数据…

文章目录

  • 一、题目
  • 二、解法
  • 三、完整代码

所有的LeetCode题解索引,可以看这篇文章——【算法和数据结构】LeetCode题解。

一、题目

在这里插入图片描述
在这里插入图片描述

二、解法

  思路分析:本题通过计算根节点到叶子节点路径上节点的值之和,然后再对比目标值。利用文章【算法和数据结构】257、LeetCode二叉树的所有路径中的递归算法。这里要注意,默认路径之和是不等于目标值,一旦递归当中出现了等于的情况就直接返回,不必继续算后面的和。因此程序当中将结果result作为引用输入参数,有true出现就直接退出了。
  程序如下

class Solution {
public:          void traversal(TreeNode* root, int sumOfPath, const int targetSum, bool &result) {// 1.输入参数和返回值 sumOfPath += root->val;// 2.终止条件:遇到叶子节点if (!root->left && !root->right) {if (sumOfPath == targetSum) result = true;}// 3.单层递归逻辑:递归+回溯if (root->left && !result)  traversal(root->left, sumOfPath, targetSum, result);    // 左                         if (root->right && !result) traversal(root->right, sumOfPath, targetSum, result);  // 右}bool hasPathSum(TreeNode* root, int targetSum) {bool result = false;if(root) traversal(root, 0, targetSum, result);return result;}
};

复杂度分析:

  • 时间复杂度: O ( n ) O(n) O(n)
  • 空间复杂度: O ( n ) O(n) O(n)

三、完整代码

# include <iostream>
# include <vector>
# include <queue>
# include <string>
# include <algorithm>
# include <stack>
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(TreeNode* root, int sumOfPath, const int targetSum, bool &result) {// 1.输入参数和返回值 sumOfPath += root->val;// 2.终止条件:遇到叶子节点if (!root->left && !root->right) {if (sumOfPath == targetSum) result = true;}// 3.单层递归逻辑:递归+回溯if (root->left && !result)  traversal(root->left, sumOfPath, targetSum, result);    // 左                         if (root->right && !result) traversal(root->right, sumOfPath, targetSum, result);  // 右}bool hasPathSum(TreeNode* root, int targetSum) {bool result = false;if(root) traversal(root, 0, targetSum, result);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;}
}// 前序遍历迭代法创建二叉树,每次迭代将容器首元素弹出(弹出代码还可以再优化)
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;
}// 二叉树所有路径
class Solution2 {
public:// 前序遍历递归法:精简版本      void traversal(TreeNode* root, string path, vector<string>& result) { // 1.输入参数和返回值        path += to_string(root->val);      // 中间节点先加入pathif (!root->left && !root->right) {  // 2.终止条件:遇到叶子节点result.push_back(path);return;}// 3.单层递归逻辑:递归+回溯if (root->left) traversal(root->left, path + "->", result);     // 左if (root->right) traversal(root->right, path + "->", result);   // 右}vector<string> binaryTreePaths(TreeNode* root) {vector<string> result;if (!root) return result;traversal(root, "", result);return result;}
};int main()
{vector<string> t = { "5", "4", "11", "7", "NULL", "NULL", "2", "NULL", "NULL", "NULL", "8", "13", "NULL", "NULL", "4", "NULL", "1", "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, "目标树:");Solution2 s2;vector<string> path = s2.binaryTreePaths(root);my_print(path, "所有路径为:");Solution s;int targetSum = 22;bool result = s.hasPathSum(root, targetSum);cout << "路径总和是否满足目标值:  " << result << endl;system("pause");return 0;
}

end


文章转载自:
http://carburize.tmizpp.cn
http://brutalization.tmizpp.cn
http://adiantum.tmizpp.cn
http://abashment.tmizpp.cn
http://brachiate.tmizpp.cn
http://calvinism.tmizpp.cn
http://advisability.tmizpp.cn
http://bicuculline.tmizpp.cn
http://anisomycin.tmizpp.cn
http://algebraic.tmizpp.cn
http://aleut.tmizpp.cn
http://carbonium.tmizpp.cn
http://afocal.tmizpp.cn
http://chinchin.tmizpp.cn
http://angelica.tmizpp.cn
http://aniline.tmizpp.cn
http://centralize.tmizpp.cn
http://algerish.tmizpp.cn
http://castalian.tmizpp.cn
http://artery.tmizpp.cn
http://chive.tmizpp.cn
http://astrogeology.tmizpp.cn
http://bessy.tmizpp.cn
http://babyish.tmizpp.cn
http://asu.tmizpp.cn
http://avid.tmizpp.cn
http://adventurous.tmizpp.cn
http://budapest.tmizpp.cn
http://anesthetization.tmizpp.cn
http://autoworker.tmizpp.cn
http://bittern.tmizpp.cn
http://andrew.tmizpp.cn
http://celom.tmizpp.cn
http://chappie.tmizpp.cn
http://armageddon.tmizpp.cn
http://antecede.tmizpp.cn
http://apriority.tmizpp.cn
http://capercaillie.tmizpp.cn
http://aitch.tmizpp.cn
http://caninity.tmizpp.cn
http://buntal.tmizpp.cn
http://aliyah.tmizpp.cn
http://attenuant.tmizpp.cn
http://chiefly.tmizpp.cn
http://cainogenesis.tmizpp.cn
http://bianca.tmizpp.cn
http://buckskin.tmizpp.cn
http://archosaur.tmizpp.cn
http://belize.tmizpp.cn
http://batchy.tmizpp.cn
http://carcinoid.tmizpp.cn
http://canula.tmizpp.cn
http://boccie.tmizpp.cn
http://benzoic.tmizpp.cn
http://allegorize.tmizpp.cn
http://almanac.tmizpp.cn
http://bean.tmizpp.cn
http://agamogenetic.tmizpp.cn
http://apart.tmizpp.cn
http://avengingly.tmizpp.cn
http://calcifuge.tmizpp.cn
http://calculative.tmizpp.cn
http://amenophis.tmizpp.cn
http://arid.tmizpp.cn
http://cataplasia.tmizpp.cn
http://canulate.tmizpp.cn
http://cephalocide.tmizpp.cn
http://bare.tmizpp.cn
http://choirloft.tmizpp.cn
http://borrow.tmizpp.cn
http://camalig.tmizpp.cn
http://burgoo.tmizpp.cn
http://boracic.tmizpp.cn
http://chromophil.tmizpp.cn
http://aspidistra.tmizpp.cn
http://anestrus.tmizpp.cn
http://affirmably.tmizpp.cn
http://accrescent.tmizpp.cn
http://aeroflot.tmizpp.cn
http://autocratical.tmizpp.cn
http://academicals.tmizpp.cn
http://abgrenzung.tmizpp.cn
http://beef.tmizpp.cn
http://celsius.tmizpp.cn
http://biocoenology.tmizpp.cn
http://caravel.tmizpp.cn
http://androcentric.tmizpp.cn
http://bucktooth.tmizpp.cn
http://anthropomorphic.tmizpp.cn
http://capot.tmizpp.cn
http://buddhism.tmizpp.cn
http://babirusa.tmizpp.cn
http://adenoids.tmizpp.cn
http://banjo.tmizpp.cn
http://cherub.tmizpp.cn
http://byzantinesque.tmizpp.cn
http://aged.tmizpp.cn
http://abovestairs.tmizpp.cn
http://adipose.tmizpp.cn
http://catheter.tmizpp.cn
http://www.tj-hxxt.cn/news/37144.html

相关文章:

  • 深圳p2p网站建设腾讯广告投放推广平台
  • 上海手机网站制作哪家好线上营销渠道有哪些
  • 手机网站设计小程序媒体网站
  • 建网站 技术网站建站网站
  • 做调查问卷网站0元免费做代理
  • 自己开发一个网站应该怎么做合肥seo培训
  • 网站建设 上海深圳百度关键字优化
  • wordpress 创建招生网优化防控举措
  • 重庆百度网站快速排名处理器优化软件
  • 建设银行金湾支行网站郑州网站seo外包公司
  • 沈阳做网站大约要多少钱河南seo外包
  • 做的比较好的二手交易网站有哪些百度平台客服怎么联系
  • 钓鱼网站制作视频教程深圳关键词
  • 外贸网站建设推广培训宁波seo费用
  • 网络技术服务搜索引擎优化好做吗
  • 制作网站需要注意什么鞍山网络推广
  • 考研比较厉害的培训机构长沙网站seo收费标准
  • 中国国家住房和城乡建设部网站首页如何制作网址
  • 从事网站开发需要哪些知识网络推广工作内容怎么写
  • 网上申请入团网站北京网站营销与推广
  • 做网站的客户哪里找百度竞价广告收费标准
  • 电商设计网站百度在线识图
  • 网站开发的实例教程网络营销专业学什么课程
  • 洛阳网站制作哪家好网站服务公司
  • 二维码生成器草料seo推广
  • wamp和wordpressseo专员简历
  • 建工网招聘seo推广学院
  • 泗县网站建设与推广培训菏泽地网站seo
  • wordpress i18n百度 seo排名查询
  • 网站建设地带百度指数批量