leetcode — binary-tree-zigzag-level-order-traversal

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Source : https://oj.leetcode.com/problems/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]
 * ]
 */
public class BinaryTreeZigzagLevelOrderTraversal {

    /**
     *
     * 按层次输出树,两层之间要首尾相接
     *
     * 类似之前那个binarytree level order traversal,使用两个list来完成
     * 只需要在添加子节点的时候判断是否需要倒序即可
     * 如果需要的话则先添加右子节点,再添加左子节点
     *
     * @param root
     * @return
     */
    public List<List<Integer>> zigzagOrder (TreeNode root) {
         List<List<Integer>> result = new ArrayList<List<Integer>>();
        if (root == null) {
            return result;
        }
        List<TreeNode> curList = new ArrayList<TreeNode>();
        List<TreeNode> nextList = new ArrayList<TreeNode>();
        curList.add(root);
        boolean isReverse = true;

        while (curList.size() > 0) {
            List<Integer> list = new ArrayList<Integer>();
            for (int i = 0; i < curList.size(); i++) {
                TreeNode node = curList.get(i);
                if (node == null) {
                    continue;
                }
                list.add(node.value);
                if (isReverse) {
                    nextList.add(node.rightChild);
                    nextList.add(node.leftChild);
                } else {
                    nextList.add(node.leftChild);
                    nextList.add(node.rightChild);
                }
            }

            if (list.size() > 0) {
                result.add(list);
            }
            curList.clear();
            curList.addAll(nextList);
            nextList.clear();
            isReverse = !isReverse;
        }
        return result;
    }


    public static void print (List<List<Integer>> lists) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("[\n");
        for (int i = 0; i < lists.size(); i++) {
            buffer.append(Arrays.toString(lists.get(i).toArray(new Integer[lists.get(i).size()])));
            buffer.append(",\n");
        }
        if (lists.size() > 0) {
            buffer.deleteCharAt(buffer.length()-2);
        }
        buffer.append("]");
        System.out.println(buffer.toString());
    }

    public TreeNode createTree (char[] treeArr) {
        TreeNode[] tree = new TreeNode[treeArr.length];
        for (int i = 0; i < treeArr.length; i++) {
            if (treeArr[i] == '#') {
                tree[i] = null;
                continue;
            }
            tree[i] = new TreeNode(treeArr[i]-'0');
        }
        int pos = 0;
        for (int i = 0; i < treeArr.length && pos < treeArr.length-1; i++) {
            if (tree[i] != null) {
                tree[i].leftChild = tree[++pos];
                if (pos < treeArr.length-1) {
                    tree[i].rightChild = tree[++pos];
                }
            }
        }
        return tree[0];
    }


    private class LevelNode {
        TreeNode node;
        int level;

        public LevelNode(TreeNode node, int level) {
            this.node = node;
            this.level = level;
        }
    }

    private class TreeNode {
        TreeNode leftChild;
        TreeNode rightChild;
        int value;

        public TreeNode(int value) {
            this.value = value;
        }

        public TreeNode() {
        }
    }


    public static void main(String[] args) {
        BinaryTreeZigzagLevelOrderTraversal traversal = new BinaryTreeZigzagLevelOrderTraversal();
        char[] arr = new char[]{'3','9','2','#','#','1','7'};

        TreeNode tree = traversal.createTree(arr);
        print(traversal.zigzagOrder(tree));
    }
}
posted @ 2017-11-09 00:47  lacker  阅读(141)  评论(0编辑  收藏  举报