当前位置:网站首页>【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;
}
}
边栏推荐
- 2020年度SaaS TOP100企业名单
- How to insist to use procedural system?
- 数字与中文大写数字互转(5千万亿亿亿亿以上的数字也支持转换)
- The latest "Grain Academy Development Tutorial" in 2022: 10 - Front-end payment module
- 【JZOF】82二叉树中和为某一值的路径(一)
- 【诗歌】爱你就像爱生命
- Force Buckle: 474. Ones and zeros
- 70. Stair Climbing Advanced Edition
- 安踏携手华为运动健康共同验证冠军跑鞋 创新引领中国体育
- linux上使用docker安装redis
猜你喜欢
![[Interface Test] Decoding the request body string of the requests library](/img/99/82ef792dacd398a8a62dd94f235a91.png)
[Interface Test] Decoding the request body string of the requests library

CV review: softmax code implementation

HUAWEI CLOUD escorts the whole process of "Wandering Ark" for the first time, creating a popular brand

&& 不是此版本的有效语句分隔符

Bi Sheng Compiler Optimization: Lazy Code Motion

2022-08-09 mysql/stonedb-慢SQL-Q16分析

如何正则匹配乱码?

HBuilder X 不能运行到内置终端

A summary of 6 common tools for cross-border e-commerce

2022年最新《谷粒学院开发教程》:10 - 前台支付模块
随机推荐
VR全景拍摄如何拍摄?如何使用拍摄器材?
Interfering with BGP routing---community attributes
【对象——对象及原型链上的属性——对象的操作方法】
JS--hashchange事件--使用/教程
生成NC文件时,报错“未定义机床”
一体化伺服电机在三轴钻孔机中的应用
力扣:322. 零钱兑换
Sqlserver restricts the ip under which accounts can access the database
软考 --- 软件工程(7)软件项目管理(下)
【哲理】读书的意义
完全背包理论
A summary of 6 common tools for cross-border e-commerce
33. Fabric通道、组织、节点、权限间关系
matplotlib散点图颜色分组图例
[JZOF] 82 binary tree with a path of a certain value (1)
“我“是一名测试/开发程序员,小孙的内心独白......
金仓数据库 KingbaseGIS 使用手册(6.2. 管理函数)
shader学习笔记(五)
[WeChat applet development (8)] Summary of audio background music playback problems
2022-08-09 mysql/stonedb-subquery performance improvement-introduction