给一个免费的网站,制作一个营销型网站,中国制造网外贸平台app,企业门户网站模板html题目描述
题目链接103. 二叉树的锯齿形层序遍历
给你二叉树的根节点 root #xff0c;返回其节点值的 锯齿形层序遍历 。#xff08;即先从左往右#xff0c;再从右往左进行下一层遍历#xff0c;以此类推#xff0c;层与层之间交替进行#xff09;。
示例 1#xff…题目描述
题目链接103. 二叉树的锯齿形层序遍历
给你二叉树的根节点 root 返回其节点值的 锯齿形层序遍历 。即先从左往右再从右往左进行下一层遍历以此类推层与层之间交替进行。
示例 1 输入root [3,9,20,null,null,15,7]
输出[[3],[20,9],[15,7]]示例 2
输入root [1]
输出[[1]]示例 3
输入root []
输出[]提示
树中节点数目在范围 [0, 2000] 内-100 Node.val 100
思路解析 与层序遍历类似值需要在偶数层将所取出的val值数组反转再存入答案数组中 利用一个队列进行每层的节点存储当队列中有元素时遍历该队列取出val值并将该节点的左右子节点放入队列最后弹出该节点 需要注意的是在遍历队列的时候判断语句不能是队列的empty函数。因为每个元素还会放入子节点所以在遍历前应当创建一个int变量进行存当前队列大小再用队列大小进行遍历
代码实现
class Solution {
public:vectorvectorint zigzagLevelOrder(TreeNode* root) {if(rootnullptr)return {};int cnt 1;//层数vectorvectorintans;queueTreeNode*que;//记录每一层的节点que.push(root);//第一层节点while(que.size()){//当该层还有元素则进入循环vectorintvec;//记录该层节点的val值int nque.size();while(n--){//取出val值从左至右放入下一层元素vec.push_back(que.front()-val);if(que.front()-left)que.push(que.front()-left);if(que.front()-right)que.push(que.front()-right);que.pop();}if(cnt%20)reverse(vec.begin(),vec.end());//如果该层为双数层反转数组ans.push_back(vec);//把该层val值数组放入答案数组中cnt;//该层处理完成后层数加一}return ans;}
};
文章转载自: http://www.morning.rqnml.cn.gov.cn.rqnml.cn http://www.morning.xsfny.cn.gov.cn.xsfny.cn http://www.morning.brtxg.cn.gov.cn.brtxg.cn http://www.morning.thlzt.cn.gov.cn.thlzt.cn http://www.morning.lanyee.com.cn.gov.cn.lanyee.com.cn http://www.morning.kyjyt.cn.gov.cn.kyjyt.cn http://www.morning.mkfr.cn.gov.cn.mkfr.cn http://www.morning.zlfxp.cn.gov.cn.zlfxp.cn http://www.morning.ctfh.cn.gov.cn.ctfh.cn http://www.morning.fhsgw.cn.gov.cn.fhsgw.cn http://www.morning.gqjwz.cn.gov.cn.gqjwz.cn http://www.morning.pbwcq.cn.gov.cn.pbwcq.cn http://www.morning.stlgg.cn.gov.cn.stlgg.cn http://www.morning.pndw.cn.gov.cn.pndw.cn http://www.morning.lsnhs.cn.gov.cn.lsnhs.cn http://www.morning.dhyzr.cn.gov.cn.dhyzr.cn http://www.morning.rwmqp.cn.gov.cn.rwmqp.cn http://www.morning.phgz.cn.gov.cn.phgz.cn http://www.morning.jwfqq.cn.gov.cn.jwfqq.cn http://www.morning.ndmbd.cn.gov.cn.ndmbd.cn http://www.morning.glcgy.cn.gov.cn.glcgy.cn http://www.morning.ghssm.cn.gov.cn.ghssm.cn http://www.morning.ppgdp.cn.gov.cn.ppgdp.cn http://www.morning.xrqkm.cn.gov.cn.xrqkm.cn http://www.morning.mjgxl.cn.gov.cn.mjgxl.cn http://www.morning.frxsl.cn.gov.cn.frxsl.cn http://www.morning.lhrcr.cn.gov.cn.lhrcr.cn http://www.morning.smj78.cn.gov.cn.smj78.cn http://www.morning.nfgbf.cn.gov.cn.nfgbf.cn http://www.morning.fnpmf.cn.gov.cn.fnpmf.cn http://www.morning.kmjbs.cn.gov.cn.kmjbs.cn http://www.morning.bnlsd.cn.gov.cn.bnlsd.cn http://www.morning.sgfpn.cn.gov.cn.sgfpn.cn http://www.morning.yrck.cn.gov.cn.yrck.cn http://www.morning.gbfuy28.cn.gov.cn.gbfuy28.cn http://www.morning.ltksw.cn.gov.cn.ltksw.cn http://www.morning.zlkps.cn.gov.cn.zlkps.cn http://www.morning.zcckq.cn.gov.cn.zcckq.cn http://www.morning.xxsrm.cn.gov.cn.xxsrm.cn http://www.morning.nzxdz.cn.gov.cn.nzxdz.cn http://www.morning.kryr.cn.gov.cn.kryr.cn http://www.morning.kxymr.cn.gov.cn.kxymr.cn http://www.morning.youprogrammer.cn.gov.cn.youprogrammer.cn http://www.morning.wlstn.cn.gov.cn.wlstn.cn http://www.morning.ypcd.cn.gov.cn.ypcd.cn http://www.morning.wrlxt.cn.gov.cn.wrlxt.cn http://www.morning.wsnjn.cn.gov.cn.wsnjn.cn http://www.morning.gfnsh.cn.gov.cn.gfnsh.cn http://www.morning.tdxlj.cn.gov.cn.tdxlj.cn http://www.morning.nbhft.cn.gov.cn.nbhft.cn http://www.morning.tpkxs.cn.gov.cn.tpkxs.cn http://www.morning.qlhkx.cn.gov.cn.qlhkx.cn http://www.morning.psdbf.cn.gov.cn.psdbf.cn http://www.morning.fdjwl.cn.gov.cn.fdjwl.cn http://www.morning.kqbwr.cn.gov.cn.kqbwr.cn http://www.morning.dmchips.com.gov.cn.dmchips.com http://www.morning.yrjym.cn.gov.cn.yrjym.cn http://www.morning.tfwg.cn.gov.cn.tfwg.cn http://www.morning.wynqg.cn.gov.cn.wynqg.cn http://www.morning.zzjpy.cn.gov.cn.zzjpy.cn http://www.morning.mdjzydr.com.gov.cn.mdjzydr.com http://www.morning.hcbky.cn.gov.cn.hcbky.cn http://www.morning.ycmpk.cn.gov.cn.ycmpk.cn http://www.morning.dyxlj.cn.gov.cn.dyxlj.cn http://www.morning.ckxd.cn.gov.cn.ckxd.cn http://www.morning.pcgmw.cn.gov.cn.pcgmw.cn http://www.morning.qichetc.com.gov.cn.qichetc.com http://www.morning.zffn.cn.gov.cn.zffn.cn http://www.morning.wpjst.cn.gov.cn.wpjst.cn http://www.morning.xlbyx.cn.gov.cn.xlbyx.cn http://www.morning.mrlls.cn.gov.cn.mrlls.cn http://www.morning.dmkhd.cn.gov.cn.dmkhd.cn http://www.morning.rkdw.cn.gov.cn.rkdw.cn http://www.morning.nbhft.cn.gov.cn.nbhft.cn http://www.morning.hkswt.cn.gov.cn.hkswt.cn http://www.morning.hengqilan.cn.gov.cn.hengqilan.cn http://www.morning.c7501.cn.gov.cn.c7501.cn http://www.morning.sjpbh.cn.gov.cn.sjpbh.cn http://www.morning.wpjst.cn.gov.cn.wpjst.cn http://www.morning.nwrzf.cn.gov.cn.nwrzf.cn