[抄题]:

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

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

Example 2

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

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

想不到“边界”应该怎么控制:用boolean变量表示方向l/r

主函数中设置t f,强制性设置好条件往左右扩展。dfs函数中设置== null, 有条件才往左右扩展,能走多深走多深。

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. dfs一直是对当前节点操作的,不是node.left/node.right
  2. 因为dfs的函数是左右分开的,不存在一了百了的情况。所以主函数也要一个点先进去,然后再进行dc。

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

想不到“边界”应该怎么控制:用boolean变量表示方向l/r

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

solution要用来新建answer对象。然后TreeNode root需要在主函数中再声明一遍。

  1. class MyCode {
  2. public static void main (String[] args) {
  3. Solution answer = new Solution();
  4. answer.root = new TreeNode(1);
  5. answer.root.right = new TreeNode(2);
  6. answer.root.right.left = new TreeNode(3);
  7. answer.root.right.right = new TreeNode(4);
  8.  
  9. List<Integer> result = answer.boundaryOfBinaryTree(answer.root);
  10. for (int i = 0; i < result.size(); i++)
  11. System.out.println("result.get(i) = " + result.get(i));
  12. }

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

  1. // package whatever; // don't place package name!
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5. import java.lang.*;
  6.  
  7. class TreeNode
  8. {
  9. int val;
  10. TreeNode left, right;
  11.  
  12. //parameter is another item
  13. TreeNode(int item) {
  14. val = item;
  15. left = right = null;
  16. }
  17. }
  18.  
  19. class Solution {
  20. TreeNode root;
  21.  
  22. public List<Integer> boundaryOfBinaryTree(TreeNode root) {
  23. //initialization
  24. List<Integer> result = new ArrayList<Integer>();
  25. //corner case
  26. if (root == null) return result;
  27. //dfs in left and right
  28. result.add(root.val);
  29. dfs(root.left, true, false, result);
  30. dfs(root.right, false, true, result);
  31. //return
  32. return result;
  33. }
  34.  
  35. public void dfs(TreeNode root, boolean lb, boolean rb, List<Integer> result) {
  36. //exit case
  37. if (root == null) return ;
  38. //add the left root
  39. if (lb) result.add(root.val);
  40. //add the mid root
  41. if (!lb && !rb && root.left == null && root.right == null)
  42. result.add(root.val);
  43. //dfs in left and right
  44. dfs(root.left, lb, rb && root.left == null, result);
  45. dfs(root.right, lb && root.left == null, rb, result);
  46. //add the right root
  47. if (rb) result.add(root.val);
  48. }
  49. }
  50.  
  51. class MyCode {
  52. public static void main (String[] args) {
  53. Solution answer = new Solution();
  54. answer.root = new TreeNode(1);
  55. answer.root.right = new TreeNode(2);
  56. answer.root.right.left = new TreeNode(3);
  57. answer.root.right.right = new TreeNode(4);
  58.  
  59. List<Integer> result = answer.boundaryOfBinaryTree(answer.root);
  60. for (int i = 0; i < result.size(); i++)
  61. System.out.println("result.get(i) = " + result.get(i));
  62. }
  63. }

545. Boundary of Binary Tree二叉树的边界的更多相关文章

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

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

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

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

  3. Leetcode 110 Balanced Binary Tree 二叉树

    判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...

  4. [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

    [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...

  5. [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  6. [LeetCode] 543. Diameter of Binary Tree 二叉树的直径

    Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...

  7. LeetCode - Boundary of Binary Tree

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

  8. [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  9. [LeetCode] Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

随机推荐

  1. 无状态http协议上用户的身份认证

    1.注册时可以使用手机短信验证码进行身份认证 2.用户每次请求不能每次都发送验证码,这时需要服务器给客户端颁发一个身份凭证(一般为一个唯一的随机数),用户每次请求时都携带身份凭证, 服务器会记录该身份 ...

  2. zabbix 与 nginx (五)

    zabbix监控nginx的大概流程为:   1:被监控端的nginx开启stub_status模块 2:通过脚本的方式获取nginx的状态值 3:修改被监控端的配置文件,Userparameter= ...

  3. zabbix之 自定义(指定特定磁盘)监控io

    引言 zabbix自带的模板,并且完成了我们的一些比较常用的监控,现在我们如果想要监控我们磁盘的IO,这时候zabbix并没有给我们提供这么一个模板,所以我们需要自己来创建一个模板来完成磁盘IO的监控 ...

  4. Redis 集群知识点及命令

    Redis 集群命令 备注 cluster nodes 查看集群包含的节点 cluster meet <ip> <port> 将 ip 和 port 所指定的节点添加到 nod ...

  5. dubbo 熔断,限流,降级

    1 写在前面 1.1 名词解释 consumer表示服务调用方 provider标示服务提供方,dubbo里面一般就这么讲. 下面的A调用B服务,一般是泛指调用B服务里面的一个接口. 1.2 拓扑图 ...

  6. js延迟

    function sleep(numberMillis) { var now = new Date(); var exitTime = now.getTime() + numberMillis; wh ...

  7. C++Primer第五版——习题答案详解(二)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第3章 字符串.向量和数组 练习3.2 一次读入一整行 #include<iost ...

  8. 如何在idea里面新建一个maven项目,然后在这个maven项目里创建多个子模块

    如何在idea里面配置maven我这里就不多说了 先新建一个maven项目作为总的管理项目 不用勾选什么,直接下一步 这样子一个普通的maven项目就创建成功了. 因为这个项目是用来管理多个子模块的, ...

  9. 杂谈1.py

    Python命名规则: 1. 组成:数字/字母/下划线 只能以字母,下划线开头 不能包含空格 避免Python关键字和函数名 简短且具有描述性 描述数据形态及支持操作 Python动态类型 变量无类型 ...

  10. shell脚本(二)

              shell脚本(二)——变量 一.定义:用来存放各种数据,编程语言组成部分 变量的命名规则: 变量名由数字 字母下划线组成 必须以字母或者下划线开头 不能使用shell里面的关键词 ...