码迷,mamicode.com
首页 > 其他好文 > 详细

Binary Tree Zigzag Level Order Traversal

时间:2015-03-14 18:32:16      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

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).

思路:

  队列层次访问

我的代码:

技术分享
public class Solution {
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        List<List<Integer>> rst = new ArrayList<List<Integer>>();
        if(root == null)    return rst;
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.offer(root);
        int count = 1;
        while(!queue.isEmpty())
        {
            int size = queue.size();
            List<Integer> list = new ArrayList<Integer>();
            for(int i = 0; i < size; i++)
            {
                TreeNode node = queue.poll();
                if(node.left != null)   queue.offer(node.left);
                if(node.right != null)  queue.offer(node.right);
                list.add(node.val);
            }
            if(count%2 == 1)
            {
                rst.add(list);
            }
            else
            {
                Collections.reverse(list);
                rst.add(list);
            }
            count++;
        }
        return rst;
    }
}
View Code

他人代码:

技术分享
public class Solution {
    public ArrayList<ArrayList<Integer>> zigzagLevelOrder(TreeNode root) {
        ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();

        if (root == null) {
            return result;
        }

        Stack<TreeNode> currLevel = new Stack<TreeNode>();
        Stack<TreeNode> nextLevel = new Stack<TreeNode>();
        Stack<TreeNode> tmp;
        
        currLevel.push(root);
        boolean normalOrder = true;

        while (!currLevel.isEmpty()) {
            ArrayList<Integer> currLevelResult = new ArrayList<Integer>();

            while (!currLevel.isEmpty()) {
                TreeNode node = currLevel.pop();
                currLevelResult.add(node.val);

                if (normalOrder) {
                    if (node.left != null) {
                        nextLevel.push(node.left);
                    }
                    if (node.right != null) {
                        nextLevel.push(node.right);
                    }
                } else {
                    if (node.right != null) {
                        nextLevel.push(node.right);
                    }
                    if (node.left != null) {
                        nextLevel.push(node.left);
                    }
                }
            }

            result.add(currLevelResult);
            tmp = currLevel;
            currLevel = nextLevel;
            nextLevel = tmp;
            normalOrder = !normalOrder;
        }

        return result;

    }
}
View Code

学习之处:

  • 虽然使用队列也能实现该问题,但是仔细想想,如果这一层的最右节点是这层的最后一个元素,那么它的right节点是下一层的最后一个访问的,如果这一层的最左节点是该层访问的最后一个元素,那么该元素的Left将是下一层访问的节点,故由此分析,使用Stack比queue要更加合理,而且如此一来,也能节约掉Queue方法下面的链表翻转的时间。

Binary Tree Zigzag Level Order Traversal

标签:

原文地址:http://www.cnblogs.com/sunshisonghit/p/4337942.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!