加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

102. Binary Tree Level Order Traversal

发布时间:2020-12-14 03:47:28 所属栏目:大数据 来源:网络整理
导读:身体不好 ? ? ? 1 class Solution { 2 ListListInteger res = new ArrayList (); 3 public ListListInteger levelOrder(TreeNode root) { 4 bfs(root,0 ); 5 return res; 6 7 } 8 9 public void bfs(TreeNode root, int n) { 10 if (root == null ) return ;

身体不好

?

?

?

 1 class Solution {
 2     List<List<Integer>> res = new ArrayList<>();
 3     public List<List<Integer>> levelOrder(TreeNode root) {
 4         bfs(root,0);
 5         return res;
 6             
 7     }
 8     
 9     public void bfs(TreeNode root,int n) {
10         if(root == null) return;
11         if(n > res.size()) {
12             res.add(new ArrayList<Integer>());
13         }
14         res.get(n).add(root.val);
15             
16         
17         bfs(root.left,n+1);
18         bfs(root.right,n+1);
19         
20     }
21       
22 }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读