右侧视角的二叉树
2016-08-19 16:31
225 查看
这题本来是不想使用遍历来做的,但是如果不这样的话也不知道这个树的高度,那么还是直接使用层序来做吧,代码如下:
class Solution { public: vector<int> rightSideView(TreeNode* root) { int dep = -1; bfs(root, dep + 1); vector<int> res; for(int i = 0; i < ret.size(); ++i){ res.push_back(ret[i][ret[i].size() - 1]); } return res; } void bfs(TreeNode * root, int depth) { if(root == NULL) return; if(depth < ret.size()){ ret[depth].push_back(root->val); }else{ vector<int>tmp; ret.push_back(tmp); ret[depth].push_back(root->val); } if(root->left) bfs(root->left, depth + 1); if(root->right) bfs(root->right, depth + 1); } private: vector<vector<int>> ret; };
相关文章推荐
- LeetCode OJ:Binary Tree Right Side View(右侧视角下的二叉树)
- LeetCode-199. Binary Tree Right Side View (JAVA)(二叉树最右侧结点)
- [LeetCode] 199. Binary Tree Right Side View 二叉树的右侧视图
- Leetcode 116 Populating Next Right Pointers in Each Node 二叉树填充next指针指向右侧结点
- 二叉树层序遍历应用:要求将所有结点加上一个m_pNext指针,指向同层右侧的结点,右侧若无结点则指向NULL,写出其中的Connect函数。
- [LeetCode] Binary Tree Right Side View 二叉树的右侧视图
- Binary Tree Right Side View 从左边视角输出二叉树的值
- Leetcode 117 Populating Next Right Pointers in Each Node II 二叉树填充next指针指向右侧结点 II
- LeetCode 199. Binary Tree Right Side View(二叉树右侧视图)
- 二叉树实现详解
- DirectX11 学习笔记2 - 添加按键事件 实现视角平移 旋转
- 数据结构实践项目——树和二叉树(2)
- 二叉树前、中、后序遍历【非递归】【LintCode测试平台】
- Swift算法之二叉树实现的方法示例
- Lint Code 二叉树中的最大路径和
- LC3视角:Kubernetes下日志采集、存储与处理技术实践
- Extjs中通过Tree加载右侧TabPanel
- CODE[VS]_3143二叉树的序遍历
- 第十周项目1—二叉树的链式存储及基本运算
- 剑指Offer:二叉树中和为某一值的路径