leetcode--Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).

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

    3
   / \
  9  20
    /  \
   15   7

return its zigzag level order traversal as:

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


题意:给定二叉树,遍历每一层。对于当前层,如果从左到右,则下一层从右到左。

分类:二叉树


解法1:层次遍历。使用一个标记来标记层的结束。每次结束将队列里面的数据保存。

/**
 * 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>> zigzagLevelOrder(TreeNode root) {
        Stack<TreeNode> stack = new Stack<TreeNode>();    
        LinkedList<List<Integer>> res = new LinkedList<List<Integer>>();    
        if(root == null) return res;    
        List<Integer> t = new ArrayList<Integer>();    
        int low = 0;    
        int high = 0;    
        int ceng = 0;    
        boolean flag=true;
        stack.add(root);    
        t.add(root.val);    
        res.add(t);    
        t = new ArrayList<Integer>();;    
        while(low<=high){                
            TreeNode cur = stack.get(low);              
            if(cur.left!=null){    
                stack.add(cur.left);    
                t.add(cur.left.val);    
                high++;    
            }    
            if(cur.right!=null){    
                stack.add(cur.right);    
                t.add(cur.right.val);    
                high++;    
            }    
            if(low==ceng){    
            	if(flag){
            	    Collections.reverse(t);
            	}
            	flag = !flag;
                if(t.size()!=0) res.add(t);;    
                t = new ArrayList<Integer>();    
                ceng = high;    
            }    
            low++;    
        }    
        return res;    
    }
}


解法2:层次遍历。使用LinkedList从而可以在头部添加节点。

使用level来做标记从而省去层次标记。

/**
 * 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>> zigzagLevelOrder(TreeNode root) {
        LinkedList<TreeNode> queue = new LinkedList<TreeNode>();//队列,用于层次遍历
        List<List<Integer>> res = new ArrayList<List<Integer>>();//结果
        List<Integer> cur = new ArrayList<Integer>();//保留当前层数据
        if(root==null) return res;
        boolean lToR = true;//是否从左到右
        int level = 1;
        queue.add(root);
        while(queue.size()>0){
            TreeNode node = queue.poll();
            if(lToR)
                cur.add(node.val);
            else
                cur.add(0,node.val);

            if(node.left != null)
                queue.add(node.left);
            if(node.right != null)
                queue.add(node.right);
            if(--level == 0){
                level = queue.size();
                res.add(new ArrayList(cur));
                cur.clear();
                lToR = !lToR;
            }
        }
        return res;
    }
}


你可能感兴趣的:(leetcode--Binary Tree Zigzag Level Order Traversal)