• 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("[
    ");
            for (int i = 0; i < lists.size(); i++) {
                buffer.append(Arrays.toString(lists.get(i).toArray(new Integer[lists.get(i).size()])));
                buffer.append(",
    ");
            }
            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));
        }
    }
    
  • 相关阅读:
    iOS macOS拼接字符串
    .NET5控制台程序使用EF连接MYSQL数据库的方法
    redis集群升级
    PostgreSQL(一) 编译安装运行
    bat Windows 批处理脚本编写
    Windows 远程Mac
    js判断是pc或移动端核心代码
    数组拆分,多用于单个数组拆分多个数组,用处swiper里面
    什么是类路径???
    达梦数据库DM8备份时归档日志不连续问题处理
  • 原文地址:https://www.cnblogs.com/sunshine-2015/p/7807075.html
Copyright © 2020-2023  润新知