leetcode--Binary Tree Level Order Traversal

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).

For example:
Given binary tree {3,9,20,#,#,15,7},

    3
   / \
  9  20
    /  \
   15   7

return its level order traversal as:

[
  [3],
  [9,20],
  [15,7]
]

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.


题意:给定二叉树。返回每层的节点值,从左到右。

分类:二叉树


解法1:层次遍历。

[java]  view plain  copy
  1. /** 
  2.  * Definition for a binary tree node. 
  3.  * public class TreeNode { 
  4.  *     int val; 
  5.  *     TreeNode left; 
  6.  *     TreeNode right; 
  7.  *     TreeNode(int x) { val = x; } 
  8.  * } 
  9.  */  
  10. public class Solution {  
  11.     public List<List<Integer>> levelOrder(TreeNode root) {  
  12.         Stack<TreeNode> stack = new Stack<TreeNode>();  
  13.         List<List<Integer>> res = new ArrayList<List<Integer>>();  
  14.         if(root == nullreturn res;  
  15.         List<Integer> t = new ArrayList<Integer>();  
  16.         int low = 0;  
  17.         int high = 0;  
  18.         int ceng = 0;  
  19.         stack.add(root);  
  20.         t.add(root.val);  
  21.         res.add(t);  
  22.         t = new ArrayList<Integer>();;  
  23.         while(low<=high){              
  24.             TreeNode cur = stack.get(low);            
  25.             if(cur.left!=null){  
  26.                 stack.add(cur.left);  
  27.                 t.add(cur.left.val);  
  28.                 high++;  
  29.             }  
  30.             if(cur.right!=null){  
  31.                 stack.add(cur.right);  
  32.                 t.add(cur.right.val);  
  33.                 high++;  
  34.             }  
  35.             if(low==ceng){  
  36.                 if(t.size()!=0) res.add(t);  
  37.                 t = new ArrayList<Integer>();  
  38.                 ceng = high;  
  39.             }  
  40.             low++;  
  41.         }  
  42.         return res;  
  43.     }  
  44. }  


解法2:思路和解法1一样,只是代码更加简洁。

[java]  view plain  copy
  1. /** 
  2.  * Definition for a binary tree node. 
  3.  * public class TreeNode { 
  4.  *     int val; 
  5.  *     TreeNode left; 
  6.  *     TreeNode right; 
  7.  *     TreeNode(int x) { val = x; } 
  8.  * } 
  9.  */  
  10. public class Solution {  
  11.     public List<List<Integer>> levelOrder(TreeNode root) {  
  12.         ArrayList<List<Integer>> res = new ArrayList<List<Integer>>();  
  13.         LinkedList<TreeNode> queue = new LinkedList<TreeNode>();  
  14.         LinkedList<Integer> curqueue = new LinkedList<Integer>();  
  15.         if(root==nullreturn res;  
  16.         int level = 1;  
  17.         queue.add(root);  
  18.         while(queue.size()>0){  
  19.             TreeNode cur = queue.poll();  
  20.             curqueue.add(cur.val);  
  21.             if(cur.left!=null){  
  22.                 queue.add(cur.left);  
  23.             }  
  24.             if(cur.right!=null){  
  25.                 queue.add(cur.right);  
  26.             }  
  27.             if(--level==0){  
  28.                 ArrayList<Integer> arr = new ArrayList<Integer>(curqueue);  
  29.                 res.add(arr);  
  30.                 level = queue.size();  
  31.                 curqueue.clear();  
  32.             }  
  33.         }  
  34.         return res;  
  35.     }  
  36. }  

原文链接http://blog.youkuaiyun.com/crazy__chen/article/details/46378491

航拍图像多类别实例分割数据集 一、基础信息 • 数据集名称:航拍图像多类别实例分割数据集 • 图片数量: 训练集:1283张图片 验证集:416张图片 总计:1699张航拍图片 • 训练集:1283张图片 • 验证集:416张图片 • 总计:1699张航拍图片 • 分类类别: 桥梁(Bridge) 田径场(GroundTrackField) 港口(Harbor) 直升机(Helicopter) 大型车辆(LargeVehicle) 环岛(Roundabout) 小型车辆(SmallVehicle) 足球场(Soccerballfield) 游泳池(Swimmingpool) 棒球场(baseballdiamond) 篮球场(basketballcourt) 飞机(plane) 船只(ship) 储罐(storagetank) 网球场(tennis_court) • 桥梁(Bridge) • 田径场(GroundTrackField) • 港口(Harbor) • 直升机(Helicopter) • 大型车辆(LargeVehicle) • 环岛(Roundabout) • 小型车辆(SmallVehicle) • 足球场(Soccerballfield) • 游泳池(Swimmingpool) • 棒球场(baseballdiamond) • 篮球场(basketballcourt) • 飞机(plane) • 船只(ship) • 储罐(storagetank) • 网球场(tennis_court) • 标注格式:YOLO格式,包含实例分割的多边形坐标,适用于实例分割任务。 • 数据格式:航拍图像数据。 二、适用场景 • 航拍图像分析系统开发:数据集支持实例分割任务,帮助构建能够自动识别和分割航拍图像中各种物体的AI模型,用于地理信息系统、环境监测等。 • 城市
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值