当前位置:网站首页>[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
边栏推荐
- 学会打字后的思考
- Minecraft 1.12.2模组开发(四十三) 自定义盾牌(Shield)
- Deep analysis of C language pointer (Part I)
- Centralized record of experimental problems
- Cmake project under vs2019: calculating binocular parallax using elas method
- Create vs project with MATLAB
- go slice
- 韩国或将禁止苹果和谷歌向开发者抽佣 创全球首例
- Linux中,MySQL的常用命令
- pikachuxss如何获取cookie靶场,返回首页总是失败
猜你喜欢

小米手机全球已舍弃“MI”品牌,全面改用“xiaomi”全称品牌

Chrome 94 introduces the controversial idle detection API, which apple and Mozilla oppose

Xiaomi mobile phone has abandoned the "Mi" brand all over the world and switched to the full name brand of "Xiaomi"

Problem brushing plan -- dynamic programming (III)

Addition, deletion, modification and query of MySQL advanced table

Express③(使用Express编写接口、跨域有关问题)

Win 11K in 100 days, super complete learning guide for job transfer test


Keywords static, extern + global and local variables

Write table of MySQL Foundation (create table)
随机推荐
MySQL进阶之表的增删改查
Recommended usage scenarios and production tools for common 60 types of charts
presto on spark 支持3.1.3记录
MySQL进阶之数据的增删改查(DML)
flomo软件推荐
Graph traversal - BFS, DFS
Reference of custom message in ROS function pack failed
Keywords static, extern + global and local variables
Selenium 显示等待WebDriverWait
3-5通过XSS获取cookie以及XSS后台管理系统的使用
Google 尝试在 Chrome 中使用 Rust
Win 11K in 100 days, super complete learning guide for job transfer test
Thinking after learning to type
On IRP from the perspective of source code
airbase 初步分析
Use 3080ti to run tensorflow GPU = 1 X version of the source code
go slice
学会打字后的思考
opencv应用——以图拼图
Send email to laravel