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] ]
/** * 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>> levelOrder(TreeNode root) { List<List<Integer>> res = new ArrayList<List<Integer>>(); if (root == null) { return res; } LinkedList<TreeNode> queue = new LinkedList<TreeNode>(); queue.add(root); int cur = 1; int next = 0; ArrayList<Integer> levelres = new ArrayList<Integer>(); while (!queue.isEmpty()) { TreeNode poll = queue.poll(); cur--; levelres.add(poll.val); if (poll.left != null) { queue.add(poll.left); next++; } if (poll.right != null) { queue.add(poll.right); next++; } if (cur == 0) { cur = next; next = 0; res.add(levelres); levelres = new ArrayList<Integer>(); } } return res; } }
相关推荐
我的个人微信公众号:Microstrong 微信公众号ID:MicrostrongAI 微信公众号介绍:Microstrong(小强)同学主要研究机器学习、深度学习、计算机视觉、智能对话系统相关内容,分享在学习过程中的...102. Binary Tree Leve
102-Binary Tree Level Order Traversal199-Binary Tree Right Side View:层次遍历的一个运用树的构造给出前中后序的序列中的两个,构造一棵树。递归。前序 parent left-child right-child中序 left-child parent ...
Binary Search Tree - Java Recursive - Java Iterative - Java Inorder 0099 Recover Binary Search Tree - Java Recursive 0101 Symmetric tree - Java Recursive - Java Iterative - C Recursive - ...
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 ...
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
js js_leetcode题解之102-binary-tree-level-order-traversal.js
python python_leetcode题解之107_Binary_Tree_Level_Order_Traversal_II
js js_leetcode题解之107-binary-tree-level-order-traversal-ii.js
Now we have a serial of numbers. Please build a Binary Search Tree with them. Then output its level-order traversal.
python python_leetcode题解之103_Binary_Tree_Zigzag_Level_Order_Traversal
在标签“BinaryTree”中,我们聚焦于二叉树相关的算法和数据结构。二叉树的其他遍历方法还包括前序遍历(Preorder Traversal,根-左-右)、中序遍历(Inorder Traversal,左-根-右)和后序遍历(Postorder Traversal...
- **Binary Tree Level Order Traversal**:二叉树的层次遍历。 - **Symmetric Tree**:判断一个二叉树是否是对称的。 - **Same Tree**:判断两棵二叉树是否相同。 - **Balanced Binary Tree**:判断一个二叉树...
leetcode卡 LeetCode 记录一下再LeetCode上刷的题,坚持每天刷一道吧 2017.06.12 打卡[LeetCode 2. Add ...Level Order Traversal II], Tree/BFS 2017.06.20 打卡[LeetCode 324. Wiggle Sort II], S
js js_leetcode题解之103-binary-tree-zigzag-level-order-traversal.js
在给定的代码中,我们讨论的是如何解决LeetCode上的一个问题——“二叉树的层次遍历II”(Binary Tree Level Order Traversal II),这个问题要求我们实现一个C++函数`levelOrderBottom`,该函数接收一个二叉树的根...