新博文地址:
[leetcode]Binary Tree Level Order Traversal
Binary Tree Level Order Traversal
写道
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
For example:
Given binary tree {3,9,20,#,#,15,7},
3
/ \
9 20
/ \
15 7
return its level order traversal as:
[
[3],
[9,20],
[15,7]
]
confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.
OJ's Binary Tree Serialization:
The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.
Here's an example:
1
/ \
2 3
/
4
\
5
The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".
For example:
Given binary tree {3,9,20,#,#,15,7},
3
/ \
9 20
/ \
15 7
return its level order traversal as:
[
[3],
[9,20],
[15,7]
]
confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.
OJ's Binary Tree Serialization:
The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.
Here's an example:
1
/ \
2 3
/
4
\
5
The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".
二叉树的层次遍历BFS,与DFS一样,是很简单,基础的树的遍历方式。需要对每一层进行分别的解析,我很直接的维护了两个队列。不啰嗦
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); public ArrayList<ArrayList<Integer>> levelOrder(TreeNode root) { if(root == null){ return result; } ArrayDeque<TreeNode> pre = new ArrayDeque<TreeNode>(); ArrayDeque<TreeNode> post = new ArrayDeque<TreeNode>(); ArrayList<Integer> list = new ArrayList<Integer>(); pre.offer(root); list.add(root.val); result.add(list); while(!pre.isEmpty()){ TreeNode tem = pre.poll(); if(tem.left != null){ post.offer(tem.left); } if(tem.right != null){ post.offer(tem.right); } if(pre.isEmpty()){//将post的值赋给pre,post清空,并将pre的val赋给list if(post.isEmpty()){ break; } pre = post.clone(); post.clear(); ArrayList<Integer> temList = new ArrayList<Integer>(); for(TreeNode node: pre){ temList.add(node.val); } result.add(temList); } } return result; }
相关推荐
我的个人微信公众号:Microstrong 微信公众号ID:MicrostrongAI 微信公众号介绍:Microstrong(小强)同学主要研究机器学习、深度学习、计算机视觉、智能对话系统相关内容,分享在学习过程中的...102. Binary Tree Leve
java java_leetcode-107-binary-tree-level-order-traversal
java java_leetcode-102-binary-tree-level-order-traversal
python python_leetcode题解之102_Binary_Tree_Level_Order_Traversal
python python_leetcode题解之107_Binary_Tree_Level_Order_Traversal_II
js js_leetcode题解之102-binary-tree-level-order-traversal.js
js js_leetcode题解之107-binary-tree-level-order-traversal-ii.js
102-Binary Tree Level Order Traversal199-Binary Tree Right Side View:层次遍历的一个运用树的构造给出前中后序的序列中的两个,构造一棵树。递归。前序 parent left-child right-child中序 left-child parent ...
python python_leetcode题解之103_Binary_Tree_Zigzag_Level_Order_Traversal
js js_leetcode题解之103-binary-tree-zigzag-level-order-traversal.js
102 Binary Tree Level Order Traversal.js(二叉树级订单Traversal.js) 103 Binary Tree Zigzag Level Order Traversal.js(二叉树之字形级别顺序Traversal.js) 104 Binary Tree.js的最大深度 105从Preorder和...
https://leetcode.com/problems/binary-tree-level-order-traversal/ Binary Tree Level Order Traversal 103 https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Binary Tree Zigzag Level ...
leetcode卡 LeetCode 记录一下再LeetCode上刷的题,坚持每天刷一道吧 2017.06.12 打卡[LeetCode 2. Add ...Level Order Traversal II], Tree/BFS 2017.06.20 打卡[LeetCode 324. Wiggle Sort II], S
leetcode what_the_dead_men_say 所以这只是一个 repo,我从leetcode.com存储我的问题解决方案。 二叉树 0098 Validate Binary Search Tree - Java Recursive - Java Iterative - Java Inorder 0099 Recover ...
- **Binary Tree Level Order Traversal**:二叉树的层次遍历。 - **Symmetric Tree**:判断一个二叉树是否是对称的。 - **Same Tree**:判断两棵二叉树是否相同。 - **Balanced Binary Tree**:判断一个二叉树...
Traversal II 144 二叉树的前序遍历 145 二叉树的后序遍历 150 逆波兰表达式求值 167 两数之和 II - 输入有序数组 199 二叉树的右视图 每天一算:Binary Tree Right Side View 203 移除链表元素 206 反转
leetcode下载 ...Traversal II 136 只出现一次的数字 2019-01-16 144 二叉树的前序遍历 145 二叉树的后序遍历 146 LRU缓存机制 LRU缓存机制 2019-01-25 Made by Jun chen 150 逆波兰表达式求值 167 两数之
[102_binary-tree-level-order-traversal.cpp] [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...
leetcode中文版车鸟 一组用python编写的算法。 种类 冒泡排序 插入排序 归并排序 桶排序 计数排序 基数排序 ...balance_binary_tree ...binary_tree_inorder_traversal ...binary_tree_level_order_traversal_I