当前位置:网站首页>【JZOF】77 Print binary tree in zigzag
【JZOF】77 Print binary tree in zigzag
2022-08-10 00:34:00 【sigh】
题目描述:
请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推.
解法:
- The first solution uses theJDK的Collections.reverse,Reverse the collection.Do not use this tool if asked during the interview,则需要换一种写法.
public class LeftRightReverseOrder {
public ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {
ArrayList<ArrayList<Integer>> ret = new ArrayList<>();
Queue<TreeNode> queue = new LinkedList<>();
queue.add(pRoot);
boolean reverse = false;
while (!queue.isEmpty()) {
ArrayList<Integer> list = new ArrayList<>();
int cnt = queue.size();
while (cnt-- > 0) {
TreeNode node = queue.poll();
if (node == null)
continue;
list.add(node.val);
queue.add(node.left);
queue.add(node.right);
}
if (reverse)
Collections.reverse(list);
reverse = !reverse;
if (list.size() != 0)
ret.add(list);
}
return ret;
}
}
- 第二种解法:
Do it in two stacks.
stack1Store odd-numbered layer nodes,The child nodes of the odd-numbered layer nodes are in the order of left and rightpush到stack2.
slack2Store even-numbered layer nodes.The child nodes of even-numbered nodes are in right-left orderpush到stack1.
public class Solution {
public ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {
ArrayList<ArrayList<Integer> > res = new ArrayList<ArrayList<Integer> >();
if (pRoot == null) {
return res;
}
// 奇数层
Stack<TreeNode> s1 = new Stack<>();
// 偶数层
Stack<TreeNode> s2 = new Stack<>();
s1.push(pRoot);
while (!s1.isEmpty() || !s2.isEmpty()) {
ArrayList<Integer> row = new ArrayList<>();
if (!s1.isEmpty()) {
while (!s1.isEmpty()) {
TreeNode node = s1.pop();
row.add(node.val);
if (node.left != null) {
s2.push(node.left);
}
if (node.right != null) {
s2.push(node.right);
}
}
} else {
while (!s2.isEmpty()) {
TreeNode node = s2.pop();
row.add(node.val);
if (node.right != null) {
s1.push(node.right);
}
if (node.left != null) {
s1.push(node.left);
}
}
}
res.add(row);
}
return res;
}
}
边栏推荐
- 数字与中文大写数字互转(5千万亿亿亿亿以上的数字也支持转换)
- Filament - Material basic graphics drawing
- Technology feast!Huayun Data brings six topics to OpenInfra Days China
- Controller层代码这么写,简洁又优雅!
- What is the stability of the quantitative trading interface system?
- 国内十大活跃报表 BI 产品深度对比及点评
- 联盟链技术应用的难点
- matplotlib散点图颜色分组图例
- tiup cluster stop
- What kind of mentality do you need to have when using the stock quantitative trading interface
猜你喜欢
生成NC文件时,报错“未定义机床”
你的手机曾经被监控过吗?
[Interface Test] Decoding the request body string of the requests library
Sun Zhengyi lost 150 billion: it was expensive at the beginning
杭电多校-Counting Stickmen-(思维+组合数+容斥)
少儿编程 电子学会图形化编程等级考试Scratch三级真题解析(判断题)2022年6月
Gumbel distribution of discrete choice model
ElasticSearcch集群
伦敦银行情中短线的支撑和阻力位
高数_复习_第4章:向量代数和空间解析几何
随机推荐
外包的水有多深?腾讯15k的外包测试岗能去吗?
Miscellaneous talk - the sorrow of programmers
complete knapsack theory
34. Fabric2.2 证书目录里各文件作用
Day 12 of learning to program
直播预告 | ICML 2022 11位一作学者在线分享神经网络,图学习等前沿研究
函数习题(下)
2022-08-09 mysql/stonedb-subquery performance improvement-introduction
2022/8/9 考试总结
iNFTnews | 迪士尼如何布局Web3
如何知道电脑开机记录?
VR全景结合小程序,为线上电商更好的服务
【诗歌】被讨厌的勇气
友元类和友元函数
上海一科技公司刷单被罚22万,揭露网络刷单灰色产业链
setter与getter访问器属性——数据驱动显示
力扣:474.一和零
How to know the computer boot record?
高手这样看现货白银走势图
多线程是同时执行多个线程的吗