[leedcode 102] 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]
]
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<Integer> seq=new ArrayList<Integer>();
        List<List<Integer>> res=new ArrayList<List<Integer>>();
        LinkedList<TreeNode> queue=new LinkedList<TreeNode>();
        if(root==null) return res;
        queue.add(root);
        while(!queue.isEmpty()){
            int len=queue.size();
            seq=new ArrayList<Integer>();
            for(int i=0;i<len;i++){
                TreeNode node=queue.remove();
                seq.add(node.val);
                if(node.left!=null)
                    queue.add(node.left);
                if(node.right!=null)
                    queue.add(node.right);
            }
            res.add(seq);
        }
        return res;
    }
}

 

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        //本题是层序遍历二叉树,一种解法:利用两个ArrayList,一个保存本层节点,另一个保存下层节点,注意两个层次的变换,level1.addAll(level2);
        //第二种思路是,使用两个变量,一个保存本层节点的数目,一个保存下一个节点的数目,所有节点都保存在一个链表里,
        //每次remove一个节点,cun--,当cun==0时,处理结果值
        //还有一种解题思路是,利用null节点分割每一层,当遇到非null,push节点。当note==null时,
        //需要处理结果http://blog.csdn.net/liping_nju/article/details/37706541
    /*    res=new ArrayList<List<Integer>>();
        seq=new ArrayList<Integer>();
        ArrayList<TreeNode> level1=new ArrayList<TreeNode>();
        if(root==null) return res;
        level1.add(root);
        while(!level1.isEmpty()){
           ArrayList<TreeNode> level2=new ArrayList<TreeNode>();
            for(TreeNode n:level1){
                seq.add(n.val);
                if(n.left!=null){
                    level2.add(n.left);
                }
                if(n.right!=null){
                    level2.add(n.right);
                }
            }
            
            res.add(new ArrayList<Integer>(seq));
            seq.clear();
            level1.clear();
            level1.addAll(level2);
        }
        return res;*/
        
        List<List<Integer>> res=new ArrayList<List<Integer>>();
        List<Integer> seq=new ArrayList<Integer>();

        if(root==null) return res;
        LinkedList<TreeNode> queue=new LinkedList<TreeNode>();//便于删除头结点,不选ArrayList
        queue.add(root);
        int nextLevcont=0;
        int curLevcont=1;
        while(!queue.isEmpty()){
            curLevcont--;
            TreeNode node=queue.remove();//删除头结点的方法
            seq.add(node.val);
            if(node.left!=null){
               queue.add(node.left);
               nextLevcont++;
            }
               
            if(node.right!=null){
                queue.add(node.right);
                nextLevcont++;
            }
            if(curLevcont==0){
               res.add(seq);
               curLevcont=nextLevcont;
               nextLevcont=0;
               seq=new ArrayList<Integer>();
            }
        }
        return res;
        
    }
}

 

posted @ 2015-07-20 21:18  ~每天进步一点点~  阅读(140)  评论(0编辑  收藏  举报