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的更多相关文章

  1. [LeetCode] Boundary of Binary Tree 二叉树的边界

    Given a binary tree, return the values of its boundary in anti-clockwise direction starting from roo ...

  2. 545. Boundary of Binary Tree二叉树的边界

    [抄题]: Given a binary tree, return the values of its boundary in anti-clockwise direction starting fr ...

  3. LeetCode - Boundary of Binary Tree

    Given a binary tree, return the values of its boundary in anti-clockwise direction starting from roo ...

  4. [LeetCode] 545. Boundary of Binary Tree 二叉树的边界

    Given a binary tree, return the values of its boundary in anti-clockwise direction starting from roo ...

  5. LeetCode 366. Find Leaves of Binary Tree

    原题链接在这里:https://leetcode.com/problems/find-leaves-of-binary-tree/#/description 题目: Given a binary tr ...

  6. 算法与数据结构基础 - 二叉树(Binary Tree)

    二叉树基础 满足这样性质的树称为二叉树:空树或节点最多有两个子树,称为左子树.右子树, 左右子树节点同样最多有两个子树. 二叉树是递归定义的,因而常用递归/DFS的思想处理二叉树相关问题,例如Leet ...

  7. [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 ...

  8. LeetCode 545----Boundary of Binary Tree

    Given a binary tree, return the values of its boundary in anti-clockwise direction starting from roo ...

  9. 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, ...

  10. 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 ...

随机推荐

  1. MySQL之 InnoDB记录结构(转自掘金小册 MySQL是怎样运行的,版权归作者所有!)

    以下内容来自掘金小册 MySQL 是怎样运行的:从根儿上理解 MySQL 版权归原作者所有! 页是MySQL中磁盘和内存交互的基本单位,也是MySQL是管理存储空间的基本单位. 指定和修改行格式的语法 ...

  2. 放弃幻想,全面拥抱Transformer:自然语言三大特征抽取器CNN/RNN/Transformer比较

    参考: https://zhuanlan.zhihu.com/p/54743941

  3. HttpPost方式调用接口的3种方式

    第一种:需要httpclient的依赖包 <dependency> <groupId>org.apache.httpcomponents</groupId> < ...

  4. 安装mysql8.0出现服务无法启动,服务没报告任何错误

    改为net start mysql80 参考https://blog.csdn.net/gzejia/article/details/82156994

  5. Codeforces1100F Ivan and Burgers 【整体二分】【线性基】

    题目分析: 一道近似的题目曾经出现在SCOI中,那题可以利用RMQ或者线段树做,这题如果用那种做法时间复杂度会是$log$三次方的. 采用一种类似于整体二分的方法可以解决这道题. 将序列的线段树模型建 ...

  6. Magento2.X 后端开发简要1

    Megento2.X 后端开发简要 根目录位置 组件的根目录是其文件夹和文件所在的组件的顶级目录.根据您安装的MaMeto开发环境,组件的根目录可以位于两个位置: 1.<Magento inst ...

  7. Django分布式任务队列celery的实践

    不使用数据库作为 Broker Broker 的选择大致有消息队列和数据库两种,这里建议尽量避免使用数据库作为 Broker,除非你的业务系统足够简单.在并发量很高的复杂系统中,大量 Workers ...

  8. The Preliminary Contest for ICPC China Nanchang National Invitational and International Silk-Road Programming Contest

    打网络赛 比赛前的准备工作要做好 确保 c++/java/python的编译器能用 打好模板,放在桌面 A. PERFECT NUMBER PROBLEM #include <cstdio> ...

  9. Unity 阴影的制作方式

    Unity阴影制作的三种方式. 方式一:Light中Shadow Type的类型 包括Hard Shadows.Soft Shadows.No Shadows:  Mesh Renderer中的属性 ...

  10. POJ - 3616 Milking Time (动态规划)

    Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that sh ...