LeetCode545.Boundary-of-Binary-Tree
1、头节点为边界节点
2、叶结点为边界节点
3、如果节点在其所在的层中是最左边或最右边,那么也是边界节点。
思路:分成三部分来做:找左边界结点、叶结点、右边界结点。
找左边界结点要遍历root的左子树,如果左孩子存在就加入vector,否则加入右孩子; 找叶结点,可以利用前序遍历,遍历结点改为判断结点是否是叶结点,是则加入;找右边界结点类似于找左边界结点,不过是其逆序,可以利用一个栈来辅助。 还要注意这三部分会有结点重合,在组合到一起的时候可以利用一个set来去掉重复的结点。注意不能在每个函数中用vector来返回结点中的值,否则无法去除重复的结点,因为树中结点的值不是唯一的,那就指针咯
#include <iostream>
#include <vector>
#include <stack>
#include <set>
using namespace std; struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
}; class Solution {
public:
void leftMost(TreeNode* root, vector<TreeNode *> &vec)
{
while(root)
{
vec.push_back(root);
if(root->left)
root = root->left;
else root = root->right;
}
} void leaf(TreeNode* root, vector<TreeNode *> &vec)
{
if(root)
{
if(!root->left && !root->right) vec.push_back(root);
if(root->left)
leaf(root->left, vec);
if(root->right)
leaf(root->right, vec);
}
} void rightMost(TreeNode* root, vector<TreeNode *> &vec)
{
stack<TreeNode *> st;
while(root)
{
st.push(root);
if(root->right)
root = root->right;
else root = root->left;
}
while(!st.empty())
{
vec.push_back(st.top());
st.pop();
}
} vector<int> boundaryOfBinaryTree(TreeNode* root) {
vector<int> ans;
if(!root) return ans;
vector<TreeNode *> tmp;
set<TreeNode *> s;
leftMost(root, tmp);
leaf(root, tmp);
rightMost(root, tmp);
for(TreeNode *p : tmp)
{
if(s.find(p) == s.end())
{
ans.push_back(p->val);
s.insert(p);
}
}
return ans;
}
};
LeetCode545.Boundary-of-Binary-Tree的更多相关文章
- [LeetCode] Boundary of Binary Tree 二叉树的边界
Given a binary tree, return the values of its boundary in anti-clockwise direction starting from roo ...
- 545. Boundary of Binary Tree二叉树的边界
[抄题]: Given a binary tree, return the values of its boundary in anti-clockwise direction starting fr ...
- LeetCode - Boundary of Binary Tree
Given a binary tree, return the values of its boundary in anti-clockwise direction starting from roo ...
- [LeetCode] 545. Boundary of Binary Tree 二叉树的边界
Given a binary tree, return the values of its boundary in anti-clockwise direction starting from roo ...
- LeetCode 366. Find Leaves of Binary Tree
原题链接在这里:https://leetcode.com/problems/find-leaves-of-binary-tree/#/description 题目: Given a binary tr ...
- 算法与数据结构基础 - 二叉树(Binary Tree)
二叉树基础 满足这样性质的树称为二叉树:空树或节点最多有两个子树,称为左子树.右子树, 左右子树节点同样最多有两个子树. 二叉树是递归定义的,因而常用递归/DFS的思想处理二叉树相关问题,例如Leet ...
- [LeetCode] 366. Find Leaves of Binary Tree 找二叉树的叶节点
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...
- LeetCode 545----Boundary of Binary Tree
Given a binary tree, return the values of its boundary in anti-clockwise direction starting from roo ...
- 331. Verify Preorder Serialization of a Binary Tree -- 判断是否为合法的先序序列
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, ...
- 105 + 106. Construct Binary Tree from Preorder and Inorder Traversal (building trees)
Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that ...
随机推荐
- libavcodev may be vulnerable or is not supported, and should be updated for play video
media.libavcodec.allow-obsolete
- centos7.4 linux 指令
1.查看版本 lsb_release -a 2.查看mysql路径 whereis mysql 3.查看编码 locale 修改语言编码 经过在网上查找资料发现,Centos 7已经不采用/etc/s ...
- react native环境搭建与生命周期
1.搭建开发环境 英文文档:http://facebook.github.io/react-native/docs/getting-started.html 中文文档:https://reactnat ...
- sopUI上手教程
1.新建项目 File-->New SOAP Project-->Project Name:填入项目名 Initial WSDL:填入项目的 web Services 2.添加WSDL ...
- 在 Angular6 中使用 HTTP 请求服务端数据
第一步 准备好api接口地址, 例如 https://api.example.com/api/ 第二步 在根组件 app.module.ts 中引入 HttpClientModule 模块. // a ...
- web.xml:<url-pattern>
web.xml 中的 <url-pattern> 是 <servlet-mapping> 或 <filter-mapping> 下的子标签. url :http:/ ...
- python之OpenCv(五)---抓取摄像头视频图像
OpenCV 可以通过 头videoCapture()方法打开摄像 摄像头变量 = cv2.VideoCapture(n) n为整数,内置摄像头为0,若有其他摄像头则依次为1,2,3,4,... ...
- Spark Standalone spark-default.conf
Example: spark.master spark://master:7077 spark.eventLog.enabled true spark.eventLog.dir hdfs://name ...
- 前端面试题整理—jQuery篇
1.为什么使用jquery,他有哪些好处? 1)轻量级.代码简洁 2)强大的选择器,出色的DOM操作封装 3)有可靠的事件处理机制 4)浏览器兼容性好 5)支持链式操作 6)支持丰富的插件 2.jqu ...
- 使用 https://git.io 缩短 a GitHub.com URL.
curl -i https://git.io -F 'url=https://develon2015.github.io' -F 'code=develon' 现在点击 http://git.io/d ...