leetcode--Binary Tree Level Order Traversal

来源:互联网 发布:最好赚钱的软件 编辑:程序博客网 时间:2024/06/01 07:39

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.csdn.net/crazy__chen/article/details/46378491