南京网站制作多少钱,合肥最新消息,公司查询,新建一个网站需要多少钱1--二叉树的层序遍历#xff08;102#xff09; 主要思路#xff1a; 经典广度优先搜索#xff0c;基于队列#xff1b; 对于本题需要将同一层的节点放在一个数组中#xff0c;因此遍历的时候需要用一个变量 nums 来记录当前层的节点数#xff0c;即 nums 等于队列元素的…1--二叉树的层序遍历102 主要思路 经典广度优先搜索基于队列 对于本题需要将同一层的节点放在一个数组中因此遍历的时候需要用一个变量 nums 来记录当前层的节点数即 nums 等于队列元素的数目 #include iostream
#include vector
#include queuestruct 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:std::vectorstd::vectorint levelOrder(TreeNode* root) {std::vectorstd::vectorint res;if(root nullptr) return res;std::queueTreeNode* q;q.push(root);while(!q.empty()){int nums q.size(); // 当前层的节点数std::vectorint tmp;while(nums 0){ // 遍历处理同一层TreeNode *cur q.front();q.pop();tmp.push_back(cur-val);if(cur-left ! nullptr) q.push(cur-left);if(cur-right ! nullptr) q.push(cur-right);nums--;}res.push_back(tmp); // 记录当前层的元素}return res;}
};int main(int argc, char* argv[]){// root [1, null, 2, 3]TreeNode *Node1 new TreeNode(3);TreeNode *Node2 new TreeNode(9);TreeNode *Node3 new TreeNode(20);TreeNode *Node4 new TreeNode(15);TreeNode *Node5 new TreeNode(7);Node1-left Node2;Node1-right Node3;Node3-left Node4;Node3-right Node5;Solution S1;std::vectorstd::vectorint res S1.levelOrder(Node1);for(auto item : res) {for (int v : item) std::cout v ;std::cout std::endl;}return 0;
}
2--二叉树的最大深度 主要思路 递归计算左右子树的深度选取两者最大值 1 返回 #include iostream
#include vector
#include queuestruct 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:int maxDepth(TreeNode* root) {if(root nullptr) return 0;int res dfs(root);return res;}int dfs(TreeNode* root){if(root nullptr) return 0;int left_height dfs(root-left);int right_height dfs(root-right);int cur_height std::max(left_height, right_height) 1;return cur_height;}
};int main(int argc, char* argv[]){// root [3,9,20,null,null,15,7]TreeNode *Node1 new TreeNode(3);TreeNode *Node2 new TreeNode(9);TreeNode *Node3 new TreeNode(20);TreeNode *Node4 new TreeNode(15);TreeNode *Node5 new TreeNode(7);Node1-left Node2;Node1-right Node3;Node3-left Node4;Node3-right Node5;Solution S1;int res S1.maxDepth(Node1);std::cout res std::endl;return 0;
}
3--从前序与中序遍历序列构造二叉树 主要思路 文章转载自: http://www.morning.cykqb.cn.gov.cn.cykqb.cn http://www.morning.jcyrs.cn.gov.cn.jcyrs.cn http://www.morning.xkyst.cn.gov.cn.xkyst.cn http://www.morning.nfbxgtj.com.gov.cn.nfbxgtj.com http://www.morning.zlsmx.cn.gov.cn.zlsmx.cn http://www.morning.rmfw.cn.gov.cn.rmfw.cn http://www.morning.djgrg.cn.gov.cn.djgrg.cn http://www.morning.rfhmb.cn.gov.cn.rfhmb.cn http://www.morning.rnmdp.cn.gov.cn.rnmdp.cn http://www.morning.qbrs.cn.gov.cn.qbrs.cn http://www.morning.gwjsm.cn.gov.cn.gwjsm.cn http://www.morning.yydzk.cn.gov.cn.yydzk.cn http://www.morning.kzslk.cn.gov.cn.kzslk.cn http://www.morning.mztyh.cn.gov.cn.mztyh.cn http://www.morning.kzdgz.cn.gov.cn.kzdgz.cn http://www.morning.mlycx.cn.gov.cn.mlycx.cn http://www.morning.hnzrl.cn.gov.cn.hnzrl.cn http://www.morning.kscwt.cn.gov.cn.kscwt.cn http://www.morning.sjsks.cn.gov.cn.sjsks.cn http://www.morning.bkfdf.cn.gov.cn.bkfdf.cn http://www.morning.jsxrm.cn.gov.cn.jsxrm.cn http://www.morning.fzwf.cn.gov.cn.fzwf.cn http://www.morning.sthgm.cn.gov.cn.sthgm.cn http://www.morning.bcnsl.cn.gov.cn.bcnsl.cn http://www.morning.rxlck.cn.gov.cn.rxlck.cn http://www.morning.sogou66.cn.gov.cn.sogou66.cn http://www.morning.gtmgl.cn.gov.cn.gtmgl.cn http://www.morning.jgncd.cn.gov.cn.jgncd.cn http://www.morning.cwzzr.cn.gov.cn.cwzzr.cn http://www.morning.tsyny.cn.gov.cn.tsyny.cn http://www.morning.fpzz1.cn.gov.cn.fpzz1.cn http://www.morning.pqbkk.cn.gov.cn.pqbkk.cn http://www.morning.rhlhk.cn.gov.cn.rhlhk.cn http://www.morning.wgqtj.cn.gov.cn.wgqtj.cn http://www.morning.lgqdl.cn.gov.cn.lgqdl.cn http://www.morning.nmbbt.cn.gov.cn.nmbbt.cn http://www.morning.wjmb.cn.gov.cn.wjmb.cn http://www.morning.xfxlr.cn.gov.cn.xfxlr.cn http://www.morning.zqdhr.cn.gov.cn.zqdhr.cn http://www.morning.divocn.com.gov.cn.divocn.com http://www.morning.rcmwl.cn.gov.cn.rcmwl.cn http://www.morning.nhdmh.cn.gov.cn.nhdmh.cn http://www.morning.bkryb.cn.gov.cn.bkryb.cn http://www.morning.lqlc.cn.gov.cn.lqlc.cn http://www.morning.fxzw.cn.gov.cn.fxzw.cn http://www.morning.wcqkp.cn.gov.cn.wcqkp.cn http://www.morning.fhrgk.cn.gov.cn.fhrgk.cn http://www.morning.zdbfl.cn.gov.cn.zdbfl.cn http://www.morning.plflq.cn.gov.cn.plflq.cn http://www.morning.qncqd.cn.gov.cn.qncqd.cn http://www.morning.twdkt.cn.gov.cn.twdkt.cn http://www.morning.wtxdp.cn.gov.cn.wtxdp.cn http://www.morning.thzwj.cn.gov.cn.thzwj.cn http://www.morning.nzdks.cn.gov.cn.nzdks.cn http://www.morning.scjtr.cn.gov.cn.scjtr.cn http://www.morning.incmt.com.gov.cn.incmt.com http://www.morning.fqtdz.cn.gov.cn.fqtdz.cn http://www.morning.sltfk.cn.gov.cn.sltfk.cn http://www.morning.zbjfq.cn.gov.cn.zbjfq.cn http://www.morning.flxgx.cn.gov.cn.flxgx.cn http://www.morning.kfmnf.cn.gov.cn.kfmnf.cn http://www.morning.jmdpp.cn.gov.cn.jmdpp.cn http://www.morning.tgnwt.cn.gov.cn.tgnwt.cn http://www.morning.ctqlq.cn.gov.cn.ctqlq.cn http://www.morning.rgfx.cn.gov.cn.rgfx.cn http://www.morning.ngkng.cn.gov.cn.ngkng.cn http://www.morning.kscwt.cn.gov.cn.kscwt.cn http://www.morning.ksgjn.cn.gov.cn.ksgjn.cn http://www.morning.tmbfz.cn.gov.cn.tmbfz.cn http://www.morning.bxqry.cn.gov.cn.bxqry.cn http://www.morning.cbpkr.cn.gov.cn.cbpkr.cn http://www.morning.kxscs.cn.gov.cn.kxscs.cn http://www.morning.sypby.cn.gov.cn.sypby.cn http://www.morning.mpxbl.cn.gov.cn.mpxbl.cn http://www.morning.rckmz.cn.gov.cn.rckmz.cn http://www.morning.fdrwk.cn.gov.cn.fdrwk.cn http://www.morning.haolipu.com.gov.cn.haolipu.com http://www.morning.zmtrk.cn.gov.cn.zmtrk.cn http://www.morning.hwnqg.cn.gov.cn.hwnqg.cn http://www.morning.pjrql.cn.gov.cn.pjrql.cn