当前位置:网站首页>[※ leetcode refers to offer 32 - II. Print binary tree II from top to bottom (simple)]
[※ leetcode refers to offer 32 - II. Print binary tree II from top to bottom (simple)]
2022-04-23 21:02:00 【Minaldo7】
subject :
Print binary tree from top to bottom , Nodes of the same layer are printed from left to right , Print each layer to a line .
for example :
Given binary tree : [3,9,20,null,null,15,7],
Return its hierarchical traversal result :
Tips :
Total number of nodes <= 1000
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-ii-lcof
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
The problem solving process :
java Recursive hierarchical traversal
from LeetCode user : Movable dummy
/** * 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());
node.get(k).add(root.val);
bianli(root.left, k+1);
bianli(root.right, k+1);
}
}
}
Execution results :
版权声明
本文为[Minaldo7]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/111/202204210544479775.html
边栏推荐
- MySQL basic collection
- Sharpness difference (SD) calculation method of image reconstruction and generation domain index
- 电脑越用越慢怎么办?文件误删除恢复方法
- Unit function expansion
- 2021-09-02 unity project uses rider to build hot change project failure record of ilruntime
- Prim、Kruskal
- MySQL进阶之表的增删改查
- Awk example skills
- mmap、munmap
猜你喜欢
Google 尝试在 Chrome 中使用 Rust
[SQL] string series 2: split a string into multiple lines according to specific characters
Chrome 94 introduces the controversial idle detection API, which apple and Mozilla oppose
Express③(使用Express编写接口、跨域有关问题)
CUDA, NVIDIA driver, cudnn download address and version correspondence
亚马逊和Epic将入驻,微软应用商城向第三方开放
C#,打印漂亮的贝尔三角形(Bell Triangle)的源程序
C, print the source program of beautiful bell triangle
Common commands of MySQL in Linux
1. Finishing huazi Mianjing -- 1
随机推荐
Reentrant function
ubutnu20安裝CenterNet
matplotlib. Pyplot partition drawing
Minecraft 1.12.2模组开发(四十三) 自定义盾牌(Shield)
go array
DeNO 1.13.2 release
ROS学习笔记-----ROS的使用教程
setInterval、setTimeout、requestAnimationFrame
Deno 1.13.2 发布
Awk example skills
PHP的Laravel与Composer部署项目时常见问题
Singleton mode
Mysql database common sense storage engine
Gsi-ecm digital platform for engineering construction management
又一款数据分析神器:Polars 真的很强大
Tencent cloud has two sides in an hour, which is almost as terrible as one side..
韩国或将禁止苹果和谷歌向开发者抽佣 创全球首例
Cmake project under vs2019: calculating binocular parallax using elas method
Thinking after learning to type
UKFslam