103. Binary Tree Zigzag Level Order Traversal

Description

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,null,null,15,7],

103. Binary Tree Zigzag Level Order Traversal_第1张图片
tree

return its zigzag level order traversal as:

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

Solution

Level-order-traversal, time O(n), space O(n)

稍微变种一下就ok啦。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List> zigzagLevelOrder(TreeNode root) {
        List> levels = new LinkedList<>();
        if (root == null) {
            return levels;
        }
        
        Queue queue = new LinkedList<>();
        queue.offer(root);
        boolean inReverse = false;
        
        while (!queue.isEmpty()) {
            int levelSize = queue.size();
            List level = new LinkedList<>();
            
            while (levelSize-- > 0) {
                TreeNode node = queue.poll();
                
                if (inReverse) {
                    level.add(0, node.val); // insert to the begining
                } else {
                    level.add(node.val);
                }
                
                if (node.left != null) {
                    queue.offer(node.left);
                }
                
                if (node.right != null) {
                    queue.offer(node.right);
                }
            }

            levels.add(level);
            inReverse = !inReverse;
        }
        
        return levels;
    }
}

Two-stack, time O(n), space O(n)

用两个stack来回折腾。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List> zigzagLevelOrder(TreeNode root) {
        List> levels = new LinkedList<>();
        if (root == null) {
            return levels;
        }
        
        Stack order = new Stack<>();
        Stack reverse = new Stack<>();
        order.push(root);
        List level = new LinkedList<>();
        
        while (!order.empty()) {
            while (!order.empty()) {
                TreeNode node = order.pop();
                level.add(node.val);

                if (node.left != null) reverse.push(node.left);
                if (node.right != null) reverse.push(node.right); 
            }
            
            levels.add(new LinkedList<>(level));
            level.clear();
            
            if (reverse.empty()) {
                break;
            }
            
            while (!reverse.empty()) {
                TreeNode node = reverse.pop();
                level.add(node.val);

                if (node.right != null) order.push(node.right);
                if (node.left != null) order.push(node.left);
            }
            
            levels.add(new LinkedList<>(level));
            level.clear();
        }
        
        return levels;
    }
}

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