当前位置:网站首页>[leetcode refers to offer 32 - III. print binary tree III from top to bottom (medium)]
[leetcode refers to offer 32 - III. print binary tree III from top to bottom (medium)]
2022-04-23 21:02:00 【Minaldo7】
subject :
Please implement a function to print binary trees in zigzag order , The first line is printed from left to right , The second layer prints from right to left , The third line is printed from left to right , Other lines and so on .
for example :
Given binary tree : [3,9,20,null,null,15,7],
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-iii-lcof
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Return its hierarchical traversal result :
The problem solving process ①:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */
class Solution {
List<List<Integer>> node = new ArrayList();
public List<List<Integer>> levelOrder(TreeNode root) {
bianli(root,0);
for(int i=1;i<node.size();i+=2){
Collections.reverse(node.get(i));
}
return node;
}
public void bianli(TreeNode root, int k){
if(root != null){
if(node.size()<=k) node.add(new ArrayList());
node.get(k).add(root.val);
bianli(root.left, k+1);
bianli(root.right, k+1);
}
}
}
Only in the Last question Added a reversal to the result of Collections.reverse(node.get(i));
Execution results ①:
The problem solving process ②:
Ideas come from LeetCode user : Mai Yuheng
Mainly used arraylist.add(int index,E element) Index from array in index=0 Insert element at .
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */
class Solution {
List<List<Integer>> node = new ArrayList();
public List<List<Integer>> levelOrder(TreeNode root) {
bianli(root,0);
return node;
}
public void bianli(TreeNode root, int k){
if(root != null){
if(node.size()<=k) node.add(new ArrayList());
if(k%2==0){
node.get(k).add(root.val);
}else{
node.get(k).add(0, root.val);
}
bianli(root.left, k+1);
bianli(root.right, k+1);
}
}
}
Execution results ②:
版权声明
本文为[Minaldo7]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/111/202204210544479734.html
边栏推荐
猜你喜欢
Zhongchuang storage | how to choose a useful distributed storage cloud disk
Write table of MySQL Foundation (create table)
DeNO 1.13.2 release
Graph traversal - BFS, DFS
Keywords static, extern + global and local variables
100天拿下11K,转岗测试的超全学习指南
Lunch on the 23rd day at home
浅谈数据库设计之三大范式
Google 尝试在 Chrome 中使用 Rust
Win 11K in 100 days, super complete learning guide for job transfer test
随机推荐
Win 11K in 100 days, super complete learning guide for job transfer test
UKFslam
小米手机全球已舍弃“MI”品牌,全面改用“xiaomi”全称品牌
[SQL] string series 2: split a string into multiple lines according to specific characters
Sharpness difference (SD) calculation method of image reconstruction and generation domain index
Send email to laravel
opencv应用——以图拼图
go map
C# 知识
Identifier CV is not defined in opencv4_ CAP_ PROP_ FPS; CV_ CAP_ PROP_ FRAME_ COUNT; CV_ CAP_ PROP_ POS_ Frames problem
Awk example skills
IOT 设计与开发
MySQL基础合集
Queue template code
MySQL进阶之表的增删改查
Minecraft 1.12.2 module development (43) custom shield
Flomo software recommendation
Chrome 94 引入具有争议的 Idle Detection API,苹果和Mozilla反对
Another data analysis artifact: Polaris is really powerful
Thinkphp5 + data large screen display effect