Binary Tree Level Order TraversalSep 29 '125832 / 14746
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 binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: vector<vector<int> > levelOrder(TreeNode *root) { vector<vector<int> > res; if (root == NULL) return res; queue<TreeNode*> q; int cur = 1, next = 0; q.push(root); vector<int> a; while (!q.empty()) { auto tmp = q.front(); q.pop(); a.push_back(tmp->val); cur--; if (tmp->left != NULL) q.push(tmp->left), next++; if (tmp->right != NULL) q.push(tmp->right), next++; if (cur == 0) { cur = next; next = 0; res.push_back(a); a.clear(); } } //if (a.size() > 0) res.push_back(a); return res; } };
相关推荐
我的个人微信公众号:Microstrong 微信公众号ID:MicrostrongAI 微信公众号介绍:Microstrong(小强)同学主要研究机器学习、深度学习、计算机视觉、智能对话系统相关内容,分享在学习过程中的...102. Binary Tree Leve
Leetcode 使用 Javascript 的解决方案Leetcode Problems and interview problems in Javascript10 Regular Expresion Matching.js100 Same Tree.js101 Symmetric Tree.js102 Binary Tree Level Order Traversal.js...
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...