- 浏览: 173637 次
- 性别:
- 来自: 济南
文章分类
最新评论
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]
]
Binary Tree Level Order Traversal的变形题目,我们只要在上一题的基础上维护一个变量level,来判断是偶数层还是奇数层,如果是奇数层我们就正序添加,如果是偶数层就倒叙添加。我们可以用链表的addFirst() 方法。代码如下:
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]
]
Binary Tree Level Order Traversal的变形题目,我们只要在上一题的基础上维护一个变量level,来判断是偶数层还是奇数层,如果是奇数层我们就正序添加,如果是偶数层就倒叙添加。我们可以用链表的addFirst() 方法。代码如下:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public List<List<Integer>> zigzagLevelOrder(TreeNode root) { List<List<Integer>> result = new ArrayList<List<Integer>>(); LinkedList<Integer> list = new LinkedList<Integer>(); Queue<TreeNode> queue = new LinkedList<TreeNode>(); if(root == null) return result; queue.offer(root); int count = 1, level = 1, helper = 0; while(!queue.isEmpty()) { TreeNode cur = queue.poll(); count --; if(level % 2 == 1) { list.add(cur.val); } else { list.addFirst(cur.val); } if(cur.left != null) { queue.offer(cur.left); helper ++; } if(cur.right != null) { queue.offer(cur.right); helper ++; } if(count == 0) { result.add(new LinkedList<Integer>(list)); count = helper; helper = 0; level ++; list.clear(); } } return result; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 227Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 226You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 342Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 334Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 451Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 512Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 431Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 620Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 428The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 388Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 519Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 533Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 371All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 861Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 883Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 555Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 601Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 764Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 736You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 629For a undirected graph with tre ...
相关推荐
Construct Binary Tree from Preorder and Inorder Traversal 根据先序,中序建立二叉树
lru缓存leetcode 1 ...Two Sum 10 ...Regular Expression Matching ...Tree ...https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Binary Tree Zigzag Level Order Traversal 104 htt
java lru leetcode what_the_dead_men_say 所以这只是一个 ... 二叉树 0098 ...Zigzag Level Order Traversal - Python3 iterative BFS w Deques 0104 Maximum depth of binary tree - Java Iterative
103 Binary Tree Zigzag Level Order Traversal.js(二叉树之字形级别顺序Traversal.js) 104 Binary Tree.js的最大深度 105从Preorder和Inorder Traversal.js构造二叉树 106从有序和后置Traversal.js构造二叉树 ...
Binary Tree Zigzag Level Order Traversal Recover Binary Search Tree Same Tree Symmetric Tree Balanced Binary Tree Flatten Binary Tree to Linked List Populating Next Right Pointers in Each Node II ...
我的个人微信公众号:Microstrong 微信公众号ID:MicrostrongAI 微信公众号介绍:Microstrong(小强)同学主要研究机器学习、深度学习、计算机视觉、智能对话系统相关内容,分享在学习过程中的...102. Binary Tree Leve
94.Binary_Tree_Inorder_Traversal二叉树的中序遍历【LeetCode单题讲解系列】
Binary_Tree_Level_Order_Traversa
Now we have a serial of numbers. Please build a Binary Search Tree with them. Then output its level-order traversal.
Construct Binary Tree from Inorder and Postorder Traversa.根据先序后续构建二叉树
An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the ...
105.construct_binary_tree_from_preorder_and_inorder_traversal从前序
BinaryTree-BinaryTree
binary_tree_traversal.py
BinaryTree-源码.rar
binary tree 的C语言实现算法,需要在linux环境下编译
144.Binary_Tree_Preorder_Traversal二叉树的前序遍历【LeetCode单题讲解系列】
145.Binary_Tree_Postorder_Traversal二叉树的后序遍历【LeetCode单题讲解系列】
懂的人自然懂:) 用C#写的binarytree
This is a binary tree search implementation.