• [LeetCode] 545. Boundary of Binary Tree


    The boundary of a binary tree is the concatenation of the root, the left boundary, the leaves ordered from left-to-right, and the reverse order of the right boundary.

    The left boundary is the set of nodes defined by the following:

    • The root node's left child is in the left boundary. If the root does not have a left child, then the left boundary is empty.
    • If a node in the left boundary and has a left child, then the left child is in the left boundary.
    • If a node is in the left boundary, has no left child, but has a right child, then the right child is in the left boundary.
    • The leftmost leaf is not in the left boundary.

    The right boundary is similar to the left boundary, except it is the right side of the root's right subtree. Again, the leaf is not part of the right boundary, and the right boundary is empty if the root does not have a right child.

    The leaves are nodes that do not have any children. For this problem, the root is not a leaf.

    Given the root of a binary tree, return the values of its boundary.

    Example 1:

    Input: root = [1,null,2,3,4]
    Output: [1,3,4,2]
    Explanation:
    - The left boundary is empty because the root does not have a left child.
    - The right boundary follows the path starting from the root's right child 2 -> 4.
      4 is a leaf, so the right boundary is [2].
    - The leaves from left to right are [3,4].
    Concatenating everything results in [1] + [] + [3,4] + [2] = [1,3,4,2].
    

    Example 2:

    Input: root = [1,2,3,4,5,6,null,null,null,7,8,9,10]
    Output: [1,2,4,7,8,9,10,6,3]
    Explanation:
    - The left boundary follows the path starting from the root's left child 2 -> 4.
      4 is a leaf, so the left boundary is [2].
    - The right boundary follows the path starting from the root's right child 3 -> 6 -> 10.
      10 is a leaf, so the right boundary is [3,6], and in reverse order is [6,3].
    - The leaves from left to right are [4,7,8,9,10].
    Concatenating everything results in [1] + [2] + [4,7,8,9,10] + [6,3] = [1,2,4,7,8,9,10,6,3]. 

    Constraints:

    • The number of nodes in the tree is in the range [1, 104].
    • -1000 <= Node.val <= 1000

    二叉树的边界。

    题意是给一个二叉树,请按逆时针方向输出二叉树边界上的节点。这个题需要分成几个部分做,分别是 root 节点,左边缘的节点,叶子节点,右边缘的节点。

    首先将根节点的值放入一个数组[1],这没什么可说的;

    再来是创建一个数组[2],用先序遍历找到所有的左边缘的节点,注意不要将左子树上的叶子节点加入,判断方法是看当前节点是否有左右孩子,如果没有,则直接 return;

    创建另一个数组[3],用后序遍历找到所有右边缘的节点,注意不要将右子树上的叶子节点加入,判断方法是看当前节点是否有左右孩子,如果没有,则直接 return;

    再创建一个数组[4],存放所有的叶子节点,判断方法是如果当前节点没有左右孩子,则他本身就是个叶子节点。

    最后拼接如上的几个数组 [1, 2, 3, 4]。

    时间O(n)

    空间O(h)

    JavaScript实现

     1 /**
     2  * @param {TreeNode} root
     3  * @return {number[]}
     4  */
     5 var boundaryOfBinaryTree = function (root) {
     6     var head = [];
     7     var left = [];
     8     var right = [];
     9     var leaves = [];
    10     var findLeft = function (root) {
    11         if (root) {
    12             // if it is a leaf node
    13             if (root.left === null && root.right === null) {
    14                 return;
    15             }
    16             left.push(root.val);
    17             if (root.left) {
    18                 findLeft(root.left);
    19             } else {
    20                 findLeft(root.right);
    21             }
    22         }
    23     }
    24 
    25     var findRight = function (root) {
    26         if (root) {
    27             // if it is a leaf node
    28             if (root.left === null && root.right === null) {
    29                 return;
    30             }
    31             if (root.right) {
    32                 findRight(root.right);
    33             } else {
    34                 findRight(root.left);
    35             }
    36             right.push(root.val);
    37         }
    38     };
    39 
    40     var findLeaves = function (root) {
    41         if (root) {
    42             if (root.left === null && root.right === null) {
    43                 leaves.push(root.val);
    44             }
    45             findLeaves(root.left);
    46             findLeaves(root.right);
    47         }
    48     };
    49 
    50     var group = function (root) {
    51         if (root) {
    52             head.push(root.val);
    53             if (root.left) {
    54                 findLeft(root.left);
    55             }
    56             if (root.right) {
    57                 findRight(root.right);
    58             }
    59             if (root.left || root.right) {
    60                 findLeaves(root);
    61             }
    62         }
    63     };
    64     group(root);
    65     return [...head, ...left, ...leaves, ...right];
    66 };

    Java实现

     1 class Solution {
     2     List<Integer> res = new ArrayList<>();
     3 
     4     public List<Integer> boundaryOfBinaryTree(TreeNode root) {
     5         // corner case
     6         if (root == null) {
     7             return res;
     8         }
     9 
    10         // normal case
    11         res.add(root.val);
    12         leftBoundary(root.left);
    13         leaves(root.left);
    14         leaves(root.right);
    15         rightBoundary(root.right);
    16         return res;
    17     }
    18 
    19     // preorder
    20     private void leftBoundary(TreeNode root) {
    21         if (root == null || (root.left == null && root.right == null)) {
    22             return;
    23         }
    24         res.add(root.val);
    25         if (root.left == null) {
    26             leftBoundary(root.right);
    27         } else {
    28             leftBoundary(root.left);
    29         }
    30     }
    31 
    32     // postorder
    33     private void rightBoundary(TreeNode root) {
    34         if (root == null || (root.left == null && root.right == null)) {
    35             return;
    36         }
    37         if (root.right == null) {
    38             rightBoundary(root.left);
    39         } else {
    40             rightBoundary(root.right);
    41         }
    42         res.add(root.val);
    43     }
    44 
    45     private void leaves(TreeNode root) {
    46         if (root == null) {
    47             return;
    48         }
    49         if (root.left == null && root.right == null) {
    50             res.add(root.val);
    51             return;
    52         }
    53         leaves(root.left);
    54         leaves(root.right);
    55     }
    56 }

    相关题目

    199. Binary Tree Right Side View

    545. Boundary of Binary Tree

    LeetCode 题目总结

  • 相关阅读:
    本周总结
    本周总结
    第四周自我总结
    结对编程
    第四周自我总结
    第三周自我总结
    本周工作安排及内容
    知识思考
    交作业。。。
    MSF过程模型
  • 原文地址:https://www.cnblogs.com/cnoodle/p/12399851.html
Copyright © 2020-2023  润新知