- 浏览: 186244 次
- 性别:
- 来自: 济南
文章分类
最新评论
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 273Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 276You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 394Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 384Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 508Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 575Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 488Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 677Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 481The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 438Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 589Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 602Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 434All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 911Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 940Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 610Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 706Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 874Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 800You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 739For a undirected graph with tre ...
相关推荐
Javascript 的解决方案Leetcode Problems and interview problems in Javascript10 Regular Expresion Matching.js100 Same Tree.js101 Symmetric Tree.js102 Binary Tree Level Order Traversal.js103 Binary Tree ...
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构造二叉树 ...
python python_leetcode题解之103_Binary_Tree_Zigzag_Level_Order_Traversal
js js_leetcode题解之103-binary-tree-zigzag-level-order-traversal.js
- “Binary Tree Zigzag Level Order Traversal(二叉树之字形层次遍历)”是图算法中对树结构的操作。 - “Merge Sorted Array(合并排序数组)”考查基本的数组操作。 - “Number of Islands(岛屿数量)”是图...
[103_binary-tree-zigzag-level-order-traversal.cpp] [104_maximum-depth-of-binary-tree.cpp] [105_construct-binary-tree-from-preorder-and-inorder-traversal.cpp] [106_construct-binary-tree-from-inorder-...
16. **之字形打印二叉树**(Zigzag Level Order Traversal) - 知识点:二叉树,队列,方向控制 - 解题思路:类似层次遍历,但在每次层次转换时改变打印方向。 以上就是小象学院刷题班3.2-4.171中涉及的主要知识...
7. 题目107 - "Binary Tree Level Order Traversal II" 本题要求对二叉树进行倒序层次遍历。Java中,可以使用队列进行层次遍历,但每次取出节点后,先将其子节点加入队列,最后再处理该节点,以达到倒序的效果。 ...
PYTHONhttps://github.com/meetpatel1311/LeetCode/blob/main/Python/103_Binary_Tree_Zigzag_Level_Order_Traversal.py 力扣问题 数字 标题 解决方案 1. 2. 3. 4. 5. 6. 7. 8. 9. 11. 12. 13. 14. 15. 16. 17. 19. ...