当前位置:网站首页>[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
边栏推荐
- On IRP from the perspective of source code
- Deno 1.13.2 发布
- Awk example skills
- Write table of MySQL Foundation (create table)
- Problem brushing plan -- dynamic programming (III)
- Unit function expansion
- 小米手机全球已舍弃“MI”品牌,全面改用“xiaomi”全称品牌
- 引入结构化并发,Swift 5.5 发布!
- Cmake project under vs2019: calculating binocular parallax using elas method
- Ubuntu 20 installing centernet
猜你喜欢
[SQL] string series 2: split a string into multiple lines according to specific characters
C, print the source program of beautiful bell triangle
Flomo software recommendation
常用60类图表使用场景、制作工具推荐
笔记本电脑卡顿怎么办?教你一键重装系统让电脑“复活”
Question brushing plan - depth first search DFS (I)
MySQL数据库常识之储存引擎
go slice
Fastdfs mind map
Problem brushing plan -- dynamic programming (IV)
随机推荐
Express ③ (use express to write interface and cross domain related issues)
Tencent cloud has two sides in an hour, which is almost as terrible as one side..
UKFslam
Awk example skills
Bracket matching -- [implementation of one-dimensional array]
Tensorflow and pytorch middle note feature map size adjustment to achieve up sampling
管道和xargs
MySQL基础之写表(创建表)
Addition, deletion, modification and query of MySQL advanced table
[SQL] string series 2: split a string into multiple lines according to specific characters
Graph traversal - BFS, DFS
go reflect
使用mbean 自动执行heap dump
ros功能包内自定义消息引用失败
Opencv reports an error. Expected PTR < CV:: UMAT > for argument '% s'‘
Norm normalization in tensorflow and pytorch of records
Leetcode-279-complete square number
CUDA, NVIDIA driver, cudnn download address and version correspondence
mmap、munmap
Queue template code