LeetCode 545. Boundary of Binary Tree - 二叉树(Binary Tree)系列题11

该博客讨论了LeetCode的一道题目,涉及如何找出二叉树的边界节点值,包括根节点、左边界、叶节点(从左到右)和右边界(从下到上)。博主解释了左边界和右边界的定义,并提供了求解这些边界的策略。
摘要由CSDN通过智能技术生成

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

题目要求找出二叉树的边界的所有节点值,二叉树的边界包括根节点,左边界从上到下顺序,叶节点从左到右顺序,右边界从下到上顺序。

根节点和叶节点简单明了,本题的难点在于求左边界和右边界,但是题目中对左边界的定义相当于告诉了我们解法,因此把左边界定义搞清楚本题也就不难了。右边界的解法跟左边界相似。

以下就是题目中对左边界的定义:

1)根节点的左子节点属于左边界,如果左子节点不存在,则左边界为空。

2)如果一个节点属于左边界,要是它有左子节点,那么它的左子节点也属于左边界。

3)如果一个节点属于左边界,要是它没有左子节点但是有右子节点,那么它的右子节点也属于左边界。

4)最左边的叶节点不属于左边界。

右边界与左边界的定义相似,区别是它处在根节点的右子树的右测。

根据以上定义,就可以分别实现两个函数来求左边界和右边界,然后再实现一个函数求所有叶节点。

class Solution:
    def boundaryOfBinaryTree(self, root: Optional[TreeNode]) -> List[int]:
        res = []
    
        def leftBoundary(node):
            if not node:
                return
            nonlocal res
            while node:
                res.append(node.val)

                if node.left:
                    node = node.left
                elif node.right:
                    node = node.right
                else:
                    break
            res.pop()
            
        def addLeaves(node):
            
            nonlocal res
            if node != root and not node.left and not node.right:
                res.append(node.val)
            if node.left:
                addLeaves(node.left)
            if node.right:
                addLeaves(node.right)
            
        def rightBoundary(node):
            if not node:
                return
            rb = []
            
            while node:
                rb.append(node.val)

                if node.right:
                    node = node.right
                elif node.left:
                    node = node.left
                else:
                    break
            
            rb.pop()
            rb.reverse()
            nonlocal res
            res += rb
                
        res.append(root.val)
        leftBoundary(root.left)
        addLeaves(root)
        rightBoundary(root.right)
        
        return res

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>