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. Axis2创建WebService服务端接口+SoupUI以及Client端demo测试调用

    第一步:引入axis2相关jar包,如果是pom项目,直接在pom文件中引入依赖就好 <dependency> <groupId>org.apache.axis2</gr ...

  2. linux 命令ls

    命令格式 ls -la /etc -a  查看所有隐藏文件 以.开头的,就是隐藏文件.改名.开头,就可以改成隐藏文件 -l  长格式显示 ]# ls -al total 8 drwxr-xr-x    ...

  3. 【坦克大战】Unity3D多人在线游戏(泰课的坦克大战--旋转的螺丝钉)

    [坦克大战]Unity3D多人在线游戏 http://www.taikr.com/my/course/937 1.NetworkManager的介绍: 说明:选择固定生成时会自动寻找有StartPos ...

  4. Day 3 下午

    依旧是组合数问题 先来看一道题 如图,一个n*m的方格中,从原点开始,每次只能向上走或者向右走,求走到点(n,m)共有多少种走法 一般做法: 一个一个写,每一个节点的种数=它左边的数量+右边的数量 显 ...

  5. Django+Vue打造购物网站(七)

    个人中心功能开发 drf文档注释 http://www.django-rest-framework.org/topics/documenting-your-api/ 动态设置serializer和pe ...

  6. cobbler批量安装操作

    打开mirrors.aliyun.com/epel http://mirrors.aliyun.com/epel/epel-release-latest-6.noarch.rpm rpm -ivh h ...

  7. 我眼中的K-近邻算法

    有一句话这样说:如果你想了解一个人,你可以从他身边的朋友开始. 如果与他交往的好友都是一些品行高尚的人,那么可以认为这个人的品行也差不了. 其实古人在这方面的名言警句,寓言故事有很多.例如:人以类聚, ...

  8. saltstack主机管理项目:主机管理项目架构设计(二)

    1.salt架构图 https://docs.saltstack.com/en/getstarted/system/plugins.html plug-ins(左边):场景可插拔 subsystem- ...

  9. mysql/mariadb主从复制

    主从复制简介 MySQL数据库的主从复制方案,是其自带的功能,并且主从复制并不是复制磁盘上的数据库文件,而是通过binlog日志复制到需要同步的从服务器上. MySQL数据库支持单向.双向.链式级联, ...

  10. 使用select为描述符设置超时

    int readable_timeo(int fd, int sec) { fd_set rset; struct timeval tv; FD_ZERO(&rset); FD_SET(fd, ...