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));
}
}

leetcode — binary-tree-zigzag-level-order-traversal的更多相关文章

  1. [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 ...

  2. [leetcode]Binary Tree Zigzag Level Order Traversal @ Python

    原题地址:http://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ 题意: Given a binary tr ...

  3. [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 ...

  4. LeetCode :: Binary Tree Zigzag Level Order Traversal [tree, BFS]

    Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...

  5. [Leetcode] Binary tree Zigzag level order traversal二叉树Z形层次遍历

    Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...

  6. leetCode :103. Binary Tree Zigzag Level Order Traversal (swift) 二叉树Z字形层次遍历

    // 103. Binary Tree Zigzag Level Order Traversal // https://leetcode.com/problems/binary-tree-zigzag ...

  7. LeetCode 103. 二叉树的锯齿形层次遍历(Binary Tree Zigzag Level Order Traversal)

    103. 二叉树的锯齿形层次遍历 103. Binary Tree Zigzag Level Order Traversal 题目描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再 ...

  8. 【LeetCode】 Binary Tree Zigzag Level Order Traversal 解题报告

    Binary Tree Zigzag Level Order Traversal [LeetCode] https://leetcode.com/problems/binary-tree-zigzag ...

  9. 【leetcode】Binary Tree Zigzag Level Order Traversal

    Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...

  10. Binary Tree Zigzag Level Order Traversal (LeetCode) 层序遍历二叉树

    题目描述: Binary Tree Zigzag Level Order Traversal AC Rate: 399/1474 My Submissions Given a binary tree, ...

随机推荐

  1. Android中SDK工具集锦

    来源:<Android 4 高级编程> Android提供的SDK中包含有很多用于设计.实现.调试应用程序的工具:比较重要的如下所述: 1. ADB工具 Android应用程序调试桥ADB ...

  2. 【转】Zookeeper 安装和配置

    转自:http://coolxing.iteye.com/blog/1871009 Zookeeper的安装和配置十分简单, 既可以配置成单机模式, 也可以配置成集群模式. 下面将分别进行介绍. 单机 ...

  3. JavaScript(八)

    闭包 什么是闭包 函数嵌套函数,内部函数可以引用外部函数的参数和变量,参数和变量不会被垃圾回收机制收回 function aaa(a){ var b = 5; function bbb(){ a++; ...

  4. Android与ios 在input上的差异

    input{ -webkit-appearance:none; }

  5. redis的线程模型是什么?

    1.面试题 redis和memcached有什么区别? redis的线程模型是什么? 为什么单线程的redis比多线程的memcached效率要高得多(为什么redis是单线程的但是还可以支撑高并发) ...

  6. nginx + springboot 配置

    1.spring boot 访问地址http://localhost:13000/test/hello 2.配置nginx.conf文件 upstream my_ngix { server local ...

  7. ubuntu16.04开机时的.local问题

    开机时显示:您的当前网络有.local域,我们不建议这样做而且这与AVAHI网络服务探测不兼容,该服务已被禁用 解决方法: 在终端输入:sudo gedit /etc/default/avahi-da ...

  8. JDK设计模式之——装饰者模式

    假定已经有三个类A,B和C他们的继承关系如下          ClassA Class B extends A  Class C extends A  想进一步扩展类B和类C的功能,新增三个方法 m ...

  9. [Swift]LeetCode443. 压缩字符串 | String Compression

    Given an array of characters, compress it in-place. The length after compression must always be smal ...

  10. 写给需要的Javaer-大数据学习路线篇

    已经更新100+篇~ 关注公众号,BAT大神带你飞~ 听说你还在写Java,看Spring,看Dubbo,今天SpringCloud, 明天Dubbo3.X新版本... 10个开发9个半在写Java后 ...