[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 ,按顺序返回组成二叉树 边界 的这些值。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/boundary-of-binary-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

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

  • 首先将根节点的值放入结果集
  • 再来是用先序遍历找到所有的左边缘的节点,注意不要将左子树上的叶子节点加入,判断方法是看当前节点是否有左右孩子,如果没有,说明节点是一个叶子节点,不能加入左边缘,直接 return;
  • 后序遍历找到所有右边缘的节点,注意不要将右子树上的叶子节点加入,判断方法是看当前节点是否有左右孩子,如果没有,说明节点是一个叶子节点,不能加入右边缘,直接 return;
  • 最后存所有的叶子节点。这里一个比较巧妙的做法是我们判断根节点root是否有左右孩子,如果有,这棵树才可能有叶子节点,否则不会有叶子节点。一个 corner case 是如果树只有根节点一个节点的时候,是没有叶子节点的。

时间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 题目总结

posted @ 2020-03-03 04:47  CNoodle  阅读(249)  评论(0编辑  收藏  举报