前言:为了后续的实习面试,开始疯狂刷题,非常欢迎志同道合的朋友一起交流。因为时间比较紧张,目前的规划是先过一遍,写出能想到的最优算法,第二遍再考虑最优或者较优的方法。如有错误欢迎指正。博主首发CSDN,mcf171专栏。这次比赛略无语,没想到前3题都可以用暴力解。

博客链接:mcf171的博客

——————————————————————————————

Given a binary tree, return the values of its boundary in anti-clockwise direction starting from root. Boundary includes left boundary, leaves, and right boundary in order without duplicate nodes.

Left boundary is defined as the path from root to the left-most node. Right boundary is defined as the path from root to the right-mostnode. If the root doesn't have left subtree or right subtree, then the root itself is left boundary or right boundary. Note this definition only applies to the input binary tree, and not applies to any subtrees.

The left-most node is defined as a leaf node you could reach when you always firstly travel to the left subtree if exists. If not, travel to the right subtree. Repeat until you reach a leaf node.

The right-most node is also defined by the same way with left and right exchanged.

Example 1

Input:1\2/ \3   4Ouput:
[1, 3, 4, 2]Explanation:
The root doesn't have left subtree, so the root itself is left boundary.
The leaves are node 3 and 4.
The right boundary are node 1,2,4. Note the anti-clockwise direction means you should output reversed right boundary.
So order them in anti-clockwise without duplicates and we have [1,3,4,2].

Example 2

Input:____1_____/          \2            3/ \          /
4   5        6   / \      / \7   8    9  10  Ouput:
[1,2,4,7,8,9,10,6,3]Explanation:
The left boundary are node 1,2,4. (4 is the left-most node according to definition)
The leaves are node 4,7,8,9,10.
The right boundary are node 1,3,6,10. (10 is the right-most node).
So order them in anti-clockwise without duplicate nodes we have [1,2,4,7,8,9,10,6,3].

这个题目写的有点不优雅,比较通俗易懂的就是先遍历左边界,再遍历子节点,再遍历右边界。

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode(int x) { val = x; }* }*/
public class Solution {private Set<TreeNode> visited = new HashSet<TreeNode>();private boolean leftBoundary = true, rightBoundary = true;public List<Integer> boundaryOfBinaryTree(TreeNode root) {List<Integer> result = new ArrayList<Integer>();if(root != null){result.add(root.val);visited.add(root);if(root.left != null){getLeft(root.left, result);}if(root.right != null){getRight(root.right, result, true);}}return result;}private void getLeft(TreeNode root, List<Integer> result){if(root == null)return;if(leftBoundary){if(!visited.contains(root)){result.add(root.val);visited.add(root);}if(root.left == null && root.right == null){leftBoundary = false;return;}}else{if(root.left == null && root.right == null && !visited.contains(root)){result.add(root.val);visited.add(root);return;}}getLeft(root.left, result);getLeft(root.right, result);}private void getRight(TreeNode root, List<Integer>result, boolean flag){if(root == null) return;if(flag){if(root.right != null){getRight(root.left, result, !flag);getRight(root.right, result, flag);}else{getRight(root.left, result, flag);}if(!visited.contains(root)){result.add(root.val);visited.add(root);}}else{if(root.right == null && root.left == null){if(!visited.contains(root)){result.add(root.val);visited.add(root);}}getRight(root.left, result, flag);getRight(root.right, result, flag);}}
}

Leetcode-545. Boundary of Binary Tree相关推荐

  1. LeetCode 545. Boundary of Binary Tree 二叉树边界

    LeetCode 545. Boundary of Binary Tree 二叉树边界 Given a binary tree, return the values of its boundary i ...

  2. leetcode 545. Boundary of Binary Tree

    leetcode 545. Boundary of Binary Tree 这一题考察的是树型的先序遍历,三角形正好是先序遍历的访问点,需要做的工作就是识别出是否为边界,在下面的代码中全靠flag变量 ...

  3. LeetCode Weekly Contest 25 之 545.Boundary of Binary Tree

    LeetCode Weekly Contest 25 赛题 本次周赛主要分为以下4道题: 507 Perfect Number (3分) 537 Complex Number Multiplicati ...

  4. LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

    LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...

  5. LeetCode——Maximum Depth of Binary Tree

    LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...

  6. 【同124】LeetCode 543. Diameter of Binary Tree

    LeetCode 543. Diameter of Binary Tree Solution1:我的答案 思路和方法就是同124题的 /*** Definition for a binary tree ...

  7. Leetcode: Maximum Depth of Binary Tree

    题目:算出二叉树的最大深度 解决方案:(1)BFS (2)DFS (1)BFS 一层一层往下搜索,一直找到最深的点,这里由于节点的val是没有用的,所以可以用来存储当前节点的深度,然后注意指针一定要初 ...

  8. LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15

    110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...

  9. LeetCode - Medium - 114. Flatten Binary Tree to Linked List

    Topic Tree Depth-first Search Description https://leetcode.com/problems/flatten-binary-tree-to-linke ...

最新文章

  1. 【】python时间处理;PYTHON定时任务合集
  2. automation服务器不能创建对象”的问题的解决方案总结大全
  3. sap 获取计划订单bapi_sapbapi的清单.doc
  4. mongodb备份还原
  5. 读写自旋锁详解:TODO
  6. Open3d之八叉树(Octree)
  7. 思科华为双向的chap
  8. Predictably Irractional - 所有权的高昂代价
  9. 直观而简单的解决方案--软件设计的永恒追求
  10. 爱希ISee人体感应器,雷达人体存在探测应用,毫米波雷达技术方案
  11. 简洁明了的个人求职简历如何写?
  12. springboot个人博客项目面试准备
  13. SpringMVC对PathVariable的特殊字符.的处理默认是文件后缀
  14. 从西天取经的九九八十一难来看Java设计模式:模板方法模式
  15. open judge1.7.14
  16. [算法课]算法考试复习范围全解
  17. tomcat 默认编码的设置
  18. 自然资源数据治理方案
  19. PowerPCB gerber out时*.rep,*.pho,*.drl,*.lst各表示什么意思,在制板时哪些文件是制板商所需要的?
  20. 《封号码罗》python爬虫之文字点选型验证码破解一次仅需2分5厘人民币(十)

热门文章

  1. python的字节码(ByteCode)
  2. Hessian RMI功能介绍
  3. xxx牌JUC学习加油奥利给001初始篇章
  4. 教你简单4步制作“截图工具”,再也不用微信或QQ截图了
  5. 大数取模运算,快速幂取模运算
  6. trace系列0 - 概述
  7. 找不到电脑C盘下的AppData文件夹怎么办?
  8. 国际快递的寄件流程是什么,寄国际快递常见的问题有哪些
  9. c++ 十六进制转字符串(ASCII码)
  10. 数据库连接失败: Could not create connection to database server. Attempted reconnect 3 times. Giving up.