当前位置: 首页> 健康> 母婴 > 常见的电子商务网站有哪些_深圳市品牌网站建设_长沙关键词优化方法_厦门人才网唯一官方网站

常见的电子商务网站有哪些_深圳市品牌网站建设_长沙关键词优化方法_厦门人才网唯一官方网站

时间:2025/7/11 8:04:29来源:https://blog.csdn.net/sinat_41679123/article/details/144608265 浏览次数:0次
常见的电子商务网站有哪些_深圳市品牌网站建设_长沙关键词优化方法_厦门人才网唯一官方网站

Description

Given the root of a perfect binary tree, reverse the node values at each odd level of the tree.

For example, suppose the node values at level 3 are [2,1,3,4,7,11,29,18], then it should become [18,29,11,7,4,3,1,2].
Return the root of the reversed tree.

A binary tree is perfect if all parent nodes have two children and all leaves are on the same level.

The level of a node is the number of edges along the path between it and the root node.

Example 1:

在这里插入图片描述

Input: root = [2,3,5,8,13,21,34]
Output: [2,5,3,8,13,21,34]
Explanation: 
The tree has only one odd level.
The nodes at level 1 are 3, 5 respectively, which are reversed and become 5, 3.

Example 2:
在这里插入图片描述

Input: root = [7,13,11]
Output: [7,11,13]
Explanation: 
The nodes at level 1 are 13, 11, which are reversed and become 11, 13.

Example 3:

Input: root = [0,1,2,0,0,0,0,1,1,1,1,2,2,2,2]
Output: [0,2,1,0,0,0,0,2,2,2,2,1,1,1,1]
Explanation: 
The odd levels have non-zero values.
The nodes at level 1 were 1, 2, and are 2, 1 after the reversal.
The nodes at level 3 were 1, 1, 1, 1, 2, 2, 2, 2, and are 2, 2, 2, 2, 1, 1, 1, 1 after the reversal.

Constraints:

The number of nodes in the tree is in the range [1, 2^14].
0 <= Node.val <= 105
root is a perfect binary tree.

Solution

Level traversal, use an extra space to store all the nodes in current level, and use 2 pointers starting from the sides to the middle, swapping the values.

Time complexity: o ( n ) o(n) o(n)
Space complexity: o ( log ⁡ n ) o(\log n) o(logn)

Code

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:def reverseOddLevels(self, root: Optional[TreeNode]) -> Optional[TreeNode]:cur_level_nodes = []queue = collections.deque([(root, 0)])cur_level = -1while queue:node, level = queue.popleft()if level != cur_level:left, right = 0, len(cur_level_nodes) - 1while left <= right:cur_level_nodes[left].val, cur_level_nodes[right].val = cur_level_nodes[right].val, cur_level_nodes[left].valleft += 1right -= 1cur_level = levelif level & 1 == 1:cur_level_nodes.append(node)else:cur_level_nodes = []if node.left:queue.append((node.left, level + 1))queue.append((node.right, level + 1))left, right = 0, len(cur_level_nodes) - 1while left <= right:cur_level_nodes[left].val, cur_level_nodes[right].val = cur_level_nodes[right].val, cur_level_nodes[left].valleft += 1right -= 1return root
关键字:常见的电子商务网站有哪些_深圳市品牌网站建设_长沙关键词优化方法_厦门人才网唯一官方网站

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: