LeetCode 545. Boundary of Binary Tree 二叉树边界
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-most node. 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   4

Ouput:
[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].

题意: 高频题,必须熟练掌握。逆时针打印二叉树边界。

解题思路:
根据观察,我们发现

  1. 当node为leftBound左边界时,node.left也是左边界
  2. 当node为leftBound左边界时,node.left为空,则node.right也可以leftBound左边界。
  3. Bottom的所有都要加入其中。
  4. rightBound也是如此。

我们可以循环调用dfs,初始化leftBound和rightBound两个boolean参数,一层层判断。先加入左边,加入bottom,然后得到两个子树加入,最后加入右边界。

代码如下:

/*** node.left is left bound if node is left bound;* node.right could also be left bound if node is left bound && node has no left child;* Same applys for right bound;* if node is left bound, add it before 2 child - pre order;* if node is right bound, add it after 2 child - post order;* A leaf node that is neither left or right bound belongs to the bottom line;*/public List<Integer> boundaryOfBinaryTree(TreeNode root) {List<Integer> res = new ArrayList<>();if (root == null) return res;res.add(root.val);getBounds(root.left, res, true, false);getBounds(root.right, res, false, true);return res;}public void getBounds(TreeNode node, List<Integer> res, boolean leftBound, boolean rightBound) {if (node == null) return;if (leftBound) {res.add(node.val);}//add bottomif(!leftBound && !rightBound && node.left == null && node.right == null) {res.add(node.val);}getBounds(node.left, res, leftBound, rightBound && node.right == null);getBounds(node.right, res, leftBound && node.left == null, rightBound);if (rightBound) {res.add(node.val);}}

LeetCode 545. Boundary of Binary Tree 二叉树边界相关推荐

  1. leetcode 545. Boundary of Binary Tree

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

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

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

  3. [LeetCode] Invert Binary Tree - 二叉树翻转系列问题

    目录: 1.Invert Binary Tree - 二叉树翻转 [递归] 题目概述: Invert a binary tree. 4/ \2 7/ \ / \ 1 3 6 9 to 4/ \7 2/ ...

  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】Minimum Depth of Binary Tree 二叉树的最小深度 java

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

  8. LeetCode:104_Maximum Depth of Binary Tree | 二叉树的最大深度 | Easy

    要求:求二叉树的深度(二叉树的深度为最远叶子节点到根节点的距离,即根节点到最远叶子节点的距离) Given a binary tree, find its maximum depth.The maxi ...

  9. LeetCode OJ:Construct Binary Tree from Preorder and Inorder Traversal(从前序以及中序遍历结果中构造二叉树)...

    Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that ...

最新文章

  1. 因用了Insert into select语句,同事被开除了!
  2. ***如何优雅的选择字体(font-family)
  3. mahout demo——本质上是基于Hadoop的分步式算法实现,比如多节点的数据合并,数据排序,网路通信的效率,节点宕机重算,数据分步式存储...
  4. 欢迎关注我的微信公众号 “我是一个假的程序猿”
  5. python形参中传入两个实参_认识Python函数的两个概念:形参与实参(16)
  6. C#穿透session隔离———Windows服务启动UI交互程序
  7. matlab 二维高斯滤波 傅里叶_机器视觉 03.2 频域低通滤波
  8. TypeScript入门教程 之 for ... of 与 for ... in
  9. haproxy和keepalived的理解(转载)
  10. 基于点特征的各位姿求解算法对比(pose-estimation-compared)
  11. 题解【luogu2045 方格取数游戏加强版】
  12. hdu 1054 Strategic Game
  13. ndows 内存诊断工具,Windows自带内存诊断工具来帮你检测电脑内存的稳定
  14. 教育数据开放平台-雄文
  15. AI CC2017安装后,安装目录里找不到amtlib.dll文件的问题
  16. empire-CVE-2018-19462
  17. git(icode)分支及发布管理方式
  18. 开发必备的文本比较工具
  19. 输出一个由*组成的三角形图案_Python中实现个性图案的方案
  20. 基于SSM大学生宿舍交电费系统

热门文章

  1. 深入Java集合学习系列:HashSet的实现原理
  2. HTML5动画软件工具编辑器 HTML5动画分类 工具推荐
  3. [Windows核心编程]堆
  4. JZOJ 3425. 能量获取
  5. C语言若干知识点归记
  6. 程序员的自我修养(转载)
  7. 解决qt程序运行时的cannot create Qt for Embedded Linux data directory: /tmp/qtembedded-0
  8. Netty 5 io.netty.util.IllegalReferenceCountException 异常
  9. 【报告分享】基础设施的新兴趋势-毕马威.pdf(附pdf下载链接)
  10. 【报告分享】2020直播电商行业研究报告.pdf(附下载链接)