102. Binary Tree Level Order Traversal
来源:互联网 发布:网络上刘皇叔是什么梗 编辑:程序博客网 时间:2024/04/30 03:19
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,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its level order traversal as:
[
[3],
[9,20],
[15,7]
]
bfs思路
/** * Definition for a binary tree node. * 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<TreeNode*> father, son; father.push_back(root); vector<vector<int>> ans; if(root == NULL) return ans; else ans.push_back({root->val}); vector<int> tmp; int cnt = 0; while(1){ if(root->left != NULL){ son.push_back(root->left); tmp.push_back(root->left->val); } if(root->right != NULL){ son.push_back(root->right); tmp.push_back(root->right->val); } ++cnt; if(cnt == father.size()){ father = son; cnt = 0; if(father.empty()) break; ans.push_back(tmp); son.clear(); tmp.clear(); } root = father[cnt]; } return ans; }};
0 0
- LeetCode 102. Binary Tree Level Order Traversal
- [LeetCode]102.Binary Tree Level Order Traversal
- [Leetcode] 102. Binary Tree Level Order Traversal
- LeetCode --- 102. Binary Tree Level Order Traversal
- [leetcode] 102.Binary Tree Level Order Traversal
- 102.Binary Tree Level Order Traversal
- 102. Binary Tree Level Order Traversal
- 【leetcode】102. Binary Tree Level Order Traversal
- 102.Binary Tree Level Order Traversal
- 102. Binary Tree Level Order Traversal
- 102. Binary Tree Level Order Traversal
- Leetcode 102. Binary Tree Level Order Traversal
- 102. Binary Tree Level Order Traversal
- 102. Binary Tree Level Order Traversal LeetCode
- 102. Binary Tree Level Order Traversal
- [leetcode] 102. Binary Tree Level Order Traversal
- 102. Binary Tree Level Order Traversal
- 102. Binary Tree Level Order Traversal
- eWorld.ASP.Report.Maker.v9.0.1.x86-DARKSiDERS
- C++学习之函数和指针
- 谈谈我对这个互联网时代的理解
- qt之读写txt
- caffe代码阅读(持续更新)
- 102. Binary Tree Level Order Traversal
- Leetcode——229. Majority Element II
- vue.js开发环境搭建
- 工厂模式Factory
- Activity的生命周期
- 运算符重载典例:Vector-维克多
- OpenResty 环境搭建
- atoi() 函数用来将字符串转换成整数(int)
- 欢迎使用CSDN-markdown编辑器