当前位置:网站首页>[※ 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
边栏推荐
- Sharpness difference (SD) calculation method of image reconstruction and generation domain index
- setInterval、setTimeout、requestAnimationFrame
- 危机即机遇,远程办公效率为何会提升?
- Leetcode-279-complete square number
- pytorch 1.7. The model saved by X training cannot be loaded in version 1.4 or earlier
- Express ③ (use express to write interface and cross domain related issues)
- Prim、Kruskal
- go reflect
- Recommended usage scenarios and production tools for common 60 types of charts
- Crisis is opportunity. Why will the efficiency of telecommuting improve?
猜你喜欢

flomo软件推荐

Thinkphp5 + data large screen display effect

Addition, deletion, modification and query of MySQL advanced table

Opencv application -- jigsaw puzzle

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

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

41. 缺失的第一个正数

C#,打印漂亮的贝尔三角形(Bell Triangle)的源程序

CUDA, NVIDIA driver, cudnn download address and version correspondence

Use 3080ti to run tensorflow GPU = 1 X version of the source code
随机推荐
UKFslam
How to play the guiding role of testing strategy
CUDA, NVIDIA driver, cudnn download address and version correspondence
Thread safe sigleton (singleton mode)
Opencv application -- jigsaw puzzle
Learn to C language fourth day
Realrange, reduce, repeat and einops in einops package layers. Rearrange and reduce in torch. Processing methods of high-dimensional data
Deep analysis of C language pointer (Part I)
PHP的Laravel与Composer部署项目时常见问题
Express ③ (use express to write interface and cross domain related issues)
The more you use the computer, the slower it will be? Recovery method of file accidental deletion
Chrome 94 引入具有争议的 Idle Detection API,苹果和Mozilla反对
The iswow64process function determines the number of program bits
Centralized record of experimental problems
MySQL进阶之表的增删改查
MySQL basic collection
1. Finishing huazi Mianjing -- 1
Zhongchuang storage | how to choose a useful distributed storage cloud disk
Win 11K in 100 days, super complete learning guide for job transfer test