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. [SimplePlayer] 2. 在屏幕上显示视频图像

    我们这里采用SDL(本文所用版本为SDL2.0.5)来进行图像输出,SDL在进行图像渲染时一般采用的会是direct3D或者opengl,SDL对它们进行了封装,不过我们这里只讨论SDL的使用,并不会 ...

  2. CentOS下安装nvm

    1安装版本管理工具git yum install git 查看git版本 git --version 2 安装Node.js版本管理工具nvm curl -o- https://raw.githubu ...

  3. 关于base64转码解码

    刚好涉及到记录一下 1.JS BASE64 解码和编码 js代码: /** * * Base64 encode / decode * * @author haitao.tu * @date 2010- ...

  4. jmeter笔记(2)--组件介绍

    1.测试计划 测试计划(Test Plan)是使用JMeter进行测试的起点,它是其它JMeter测试元件的容器. 2.Threads(Users)-线程组 每个测试需求的必备组件,是用来模拟用户并发 ...

  5. MyBatis小问题(1)-Mapper中错误No constructor found...

    前两天又被公司叫去修改其他产品的一些问题了,没有看java相关的,今天周六,看了看MyBatis东西. 就是简单的在MySql中建了个users表,很简单,包含id,name,age,写了个bean. ...

  6. saltstack主机管理项目:主机管理项目需求分析(一)

    1.场景: 我现在又一台裸机要实现一下任务 2.配置管理: 1.装上nginx,mysql 2.nginx用我指定的配置文件 3.mysql用户 4.设置一个默认的数据库访问权限 5.启动mysql ...

  7. CMDB资产管理系统开发【day27】:理解RESTful架构

    理解RESTful架构 越来越多的人开始意识到,网站即软件,而且是一种新型的软件. 这种"互联网软件"采用客户端/服务器模式,建立在分布式体系上,通过互联网通信,具有高延时(hig ...

  8. python GUI 之 tkinter

    写一个 登陆窗口来学习 tkinter ,还剩下一些问题 代码暂时如下 import tkinter as tk import webbrowser import pickle from tkinte ...

  9. Pandas的一些简单函数总结

    pd.Series(my_list) # 从一个可迭代的对象 my_list 中创建一个数据组df.index = pd.date_range('2017/1/1', periods=df.shape ...

  10. 不定参数对arguments对象的影响

    如果声明函数时定义了不定参数,则在函数被调用时,arguments对象包含了所有传入的参数: function checkArgs(...args){ console.log(args.length, ...