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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值