LeetCode-2415. Reverse Odd Levels of Binary Tree [C++][Java]

LeetCode-2415. Reverse Odd Levels of Binary TreeReverse Odd Levels of Binary Tree - 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:[https://assets.leetcode.com/uploads/2022/07/28/first_case1.png]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:[https://assets.leetcode.com/uploads/2022/07/28/second_case3.png]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, 214]. * 0 <= Node.val <= 105 * root is a perfect binary tree.icon-default.png?t=M85Bhttps://leetcode.com/problems/reverse-odd-levels-of-binary-tree/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 <= 10^5
  • root is a perfect binary tree.

 

 【C++】

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* reverseOddLevels(TreeNode* root) {
        if (root != nullptr) {
            bfs(root->left, root->right, 1);
        }
        return root;
    }

    void bfs(TreeNode* left, TreeNode* right, int level) {
        if (left == nullptr || right == nullptr) {
            return;
        }

        if (level % 2 == 1) {
            swap(left->val, right->val);
        }

        bfs(left->left, right->right, level + 1);
        bfs(left->right, right->left, level + 1);
    }
};

【Java】

/**
 * 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 TreeNode reverseOddLevels(TreeNode root) {
        if (root != null) {
            bfs(root.left, root.right, 1);
        }
        return root;
    }

    private void bfs(TreeNode left, TreeNode right, int level) {
        if (left == null || right == null) {
            return;
        }

        if (level % 2 == 1) {
            int tmp = left.val;
            left.val = right.val;
            right.val = tmp;
        }

        bfs(left.left, right.right, level + 1);
        bfs(left.right, right.left, level + 1);
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贫道绝缘子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值