From 87cedc03bb23bd4d17722c7e1b0acaccafc5afed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BD=A9=E8=BE=95=E9=BE=99=E5=84=BF?= Date: Thu, 9 Feb 2023 11:24:09 +0800 Subject: [PATCH] =?UTF-8?q?545:=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84?= =?UTF-8?q?=E8=BE=B9=E7=95=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../editor/cn/BoundaryOfBinaryTree.java | 148 ++++++++++++++++++ .../cn/doc/content/BoundaryOfBinaryTree.md | 51 ++++++ 2 files changed, 199 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/BoundaryOfBinaryTree.java create mode 100644 src/main/java/leetcode/editor/cn/doc/content/BoundaryOfBinaryTree.md diff --git a/src/main/java/leetcode/editor/cn/BoundaryOfBinaryTree.java b/src/main/java/leetcode/editor/cn/BoundaryOfBinaryTree.java new file mode 100644 index 0000000..c2c5fe8 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/BoundaryOfBinaryTree.java @@ -0,0 +1,148 @@ +//

二叉树的 边界 是由 根节点 左边界 、按从左到右顺序的 叶节点逆序的右边界 ,按顺序依次连接组成。

+// +//

左边界 是满足下述定义的节点集合:

+// +// +// +//

右边界 定义方式与 左边界 相同,只是将左替换成右。即,右边界是根节点右子树的右侧部分;叶节点 不是 右边界的组成部分;如果根节点不含右子节点,那么右边界为

+// +//

叶节点 是没有任何子节点的节点。对于此问题,根节点 不是 叶节点。

+// +//

给你一棵二叉树的根节点 root ,按顺序返回组成二叉树 边界 的这些值。

+// +//

 

+// +//

示例 1:

+// +//
+//输入:root = [1,null,2,3,4]
+//输出:[1,3,4,2]
+//解释:
+//- 左边界为空,因为二叉树不含左子节点。
+//- 右边界是 [2] 。从根节点的右子节点开始的路径为 2 -> 4 ,但 4 是叶节点,所以右边界只有 2 。
+//- 叶节点从左到右是 [3,4] 。
+//按题目要求依序连接得到结果 [1] + [] + [3,4] + [2] = [1,3,4,2] 。
+// +//

示例 2:

+// +//
+//输入:root = [1,2,3,4,5,6,null,null,null,7,8,9,10]
+//输出:[1,2,4,7,8,9,10,6,3]
+//解释:
+//- 左边界为 [2] 。从根节点的左子节点开始的路径为 2 -> 4 ,但 4 是叶节点,所以左边界只有 2 。
+//- 右边界是 [3,6] ,逆序为 [6,3] 。从根节点的右子节点开始的路径为 3 -> 6 -> 10 ,但 10 是叶节点。
+//- 叶节点从左到右是 [4,7,8,9,10]
+//按题目要求依序连接得到结果 [1] + [2] + [4,7,8,9,10] + [6,3] = [1,2,4,7,8,9,10,6,3] 。
+// +//

 

+// +//

提示:

+// +// +// +//
Related Topics
  • 深度优先搜索
  • 二叉树

  • 👍 100
  • 👎 0
  • +package leetcode.editor.cn; + +import com.code.leet.entiy.TreeNode; + +import java.util.ArrayList; +import java.util.List; + +// 545:二叉树的边界 +public class BoundaryOfBinaryTree { + public static void main(String[] args) { + Solution solution = new BoundaryOfBinaryTree().new Solution(); + // TO TEST + } + //leetcode submit region begin(Prohibit modification and deletion) + + /** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ + class Solution { + public List boundaryOfBinaryTree(TreeNode root) { + List list = new ArrayList<>(); + list.add(root.val); + if (root.left == null && root.right == null) { + return list; + } + if (root.left != null) { + list.addAll(getLeft(root.left)); + } + + list.addAll(getLeaf(root)); + + if (root.right != null) { + List rightList = getRight(root.right); + for (int i = rightList.size() - 1; i >= 0; i--) { + list.add(rightList.get(i)); + } + } + return list; + } + + private List getLeft(TreeNode node) { + List list = new ArrayList<>(); + if (node.left == null && node.right == null) { + return new ArrayList<>(); + } + list.add(node.val); + if (node.left != null) { + list.addAll(getLeft(node.left)); + } else { + list.addAll(getLeft(node.right)); + } + return list; + } + + private List getRight(TreeNode node) { + List list = new ArrayList<>(); + if (node.left == null && node.right == null) { + return new ArrayList<>(); + } + list.add(node.val); + if (node.right != null) { + list.addAll(getRight(node.right)); + } else { + list.addAll(getRight(node.left)); + } + return list; + } + + private List getLeaf(TreeNode node) { + List list = new ArrayList<>(); + if (node.left == null && node.right == null) { + list.add(node.val); + } + if (node.left != null) { + list.addAll(getLeaf(node.left)); + } + if (node.right != null) { + list.addAll(getLeaf(node.right)); + } + return list; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} diff --git a/src/main/java/leetcode/editor/cn/doc/content/BoundaryOfBinaryTree.md b/src/main/java/leetcode/editor/cn/doc/content/BoundaryOfBinaryTree.md new file mode 100644 index 0000000..854f9d7 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/doc/content/BoundaryOfBinaryTree.md @@ -0,0 +1,51 @@ +

    二叉树的 边界 是由 根节点 左边界 、按从左到右顺序的 叶节点逆序的右边界 ,按顺序依次连接组成。

    + +

    左边界 是满足下述定义的节点集合:

    + +
      +
    • 根节点的左子节点在左边界中。如果根节点不含左子节点,那么左边界就为
    • +
    • 如果一个节点在左边界中,并且该节点有左子节点,那么它的左子节点也在左边界中。
    • +
    • 如果一个节点在左边界中,并且该节点 不含 左子节点,那么它的右子节点就在左边界中。
    • +
    • 最左侧的叶节点 不在 左边界中。
    • +
    + +

    右边界 定义方式与 左边界 相同,只是将左替换成右。即,右边界是根节点右子树的右侧部分;叶节点 不是 右边界的组成部分;如果根节点不含右子节点,那么右边界为

    + +

    叶节点 是没有任何子节点的节点。对于此问题,根节点 不是 叶节点。

    + +

    给你一棵二叉树的根节点 root ,按顺序返回组成二叉树 边界 的这些值。

    + +

     

    + +

    示例 1:

    + +
    +输入:root = [1,null,2,3,4]
    +输出:[1,3,4,2]
    +解释:
    +- 左边界为空,因为二叉树不含左子节点。
    +- 右边界是 [2] 。从根节点的右子节点开始的路径为 2 -> 4 ,但 4 是叶节点,所以右边界只有 2 。
    +- 叶节点从左到右是 [3,4] 。
    +按题目要求依序连接得到结果 [1] + [] + [3,4] + [2] = [1,3,4,2] 。
    + +

    示例 2:

    + +
    +输入:root = [1,2,3,4,5,6,null,null,null,7,8,9,10]
    +输出:[1,2,4,7,8,9,10,6,3]
    +解释:
    +- 左边界为 [2] 。从根节点的左子节点开始的路径为 2 -> 4 ,但 4 是叶节点,所以左边界只有 2 。
    +- 右边界是 [3,6] ,逆序为 [6,3] 。从根节点的右子节点开始的路径为 3 -> 6 -> 10 ,但 10 是叶节点。
    +- 叶节点从左到右是 [4,7,8,9,10]
    +按题目要求依序连接得到结果 [1] + [2] + [4,7,8,9,10] + [6,3] = [1,2,4,7,8,9,10,6,3] 。
    + +

     

    + +

    提示:

    + +
      +
    • 树中节点的数目在范围 [1, 104]
    • +
    • -1000 <= Node.val <= 1000
    • +
    + +
    Related Topics
  • 深度优先搜索
  • 二叉树

  • 👍 100
  • 👎 0
  • \ No newline at end of file