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. wamp apache 设置多端口

  2. Idea下的springboot mysql8.0等报错解决随笔

    cannot load jdbc class path:mysql8.0装载失败,可能原因,驱动名称错误,连接字符串中需要加入时区UTC,否则8.0一定会报错无法连接,关闭SSL 在applicati ...

  3. 封装redis

    封装redis import redis # r = redis.Redis() class MyRedis(): def __init__(self,ip,password,port=6379,db ...

  4. java课程之团队开发冲刺阶段1.6

    一.总结昨天进度 1.依照视频学习了sqlite,但是由于视频的不完整性导致并不知道代码的实际效果怎么样. 二.遇到的问题 1.依据上一条,在date目录下date文件夹中,的确发现了数据库的文件,但 ...

  5. List集合和JSON互转工具类

    public class JsonListUtil { /** * List<T> 转 json 保存到数据库 */ public static <T> String list ...

  6. Python必学Django框架,入门到精通学习视频教程全都在这可以领

    “人生苦短,我用python”,学python的小伙伴应该都了解这句话的含义.但是,学python,你真正了了解强大的Django框架吗!? 据说Django还是由吉普赛的一个吉他手的名字命名的呢,有 ...

  7. Java语言

    Java语言基础教程 本文将放入菜单栏中方便学习,记得点赞哦! Java分为3个体系,为JavaSE,JavaEE,JavaME,是一种面向对象的程序设计语言,记住Oracle公司收购了 Sum公司, ...

  8. [Swift]LeetCode97. 交错字符串 | Interleaving String

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Example 1: Input: s1 = ...

  9. [Swift]LeetCode692. 前K个高频单词 | Top K Frequent Words

    Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted b ...

  10. [Swift]LeetCode892. 三维形体的表面积 | Surface Area of 3D Shapes

    On a N * N grid, we place some 1 * 1 * 1 cubes. Each value v = grid[i][j] represents a tower of v cu ...