mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
0102.二叉树的层序遍历.md Java
This commit is contained in:
@ -419,6 +419,36 @@ public:
|
||||
|
||||
Java:
|
||||
|
||||
```Java
|
||||
|
||||
class Solution {
|
||||
public List<List<Integer>> resList=new ArrayList<List<Integer>>();
|
||||
public List<List<Integer>> levelOrder(TreeNode root) {
|
||||
checkFun01(root,0);
|
||||
|
||||
return resList;
|
||||
}
|
||||
|
||||
//递归方式
|
||||
public void checkFun01(TreeNode node,Integer deep){
|
||||
if(node==null) return;
|
||||
deep++;
|
||||
|
||||
if(resList.size()<deep){
|
||||
//当层级增加时,list的Item也增加,利用list的索引值进行层级界定
|
||||
List<Integer> item=new ArrayList<Integer>();
|
||||
resList.add(item);
|
||||
}
|
||||
resList.get(deep-1).add(node.val);
|
||||
|
||||
|
||||
checkFun01(node.left,deep);
|
||||
checkFun01(node.right,deep);
|
||||
}
|
||||
|
||||
|
||||
```
|
||||
|
||||
|
||||
Python:
|
||||
|
||||
|
Reference in New Issue
Block a user