【力扣每日一题】力扣2641二叉树的堂兄弟结点II

题目来源

力扣2641二叉树的堂兄弟结点II

题目概述

给你一棵二叉树的根 root ,请你将每个节点的值替换成该节点的所有 堂兄弟节点值的和 。

如果两个节点在树中有相同的深度且它们的父节点不同,那么它们互为 堂兄弟 。

请你返回修改值之后,树的根 root 。

注意,一个节点的深度指的是从树根节点到这个节点经过的边数。

思路分析

可以使用层序遍历的方式遍历树,每次遍历都能得到一层的数据,把该层的两个孩子节点val相加减去一个点的两个孩子节点val之和,就是新的数据。

代码实现

java实现

public class Solution {
    public TreeNode replaceValueInTree(TreeNode root) {
        root.val = 0;
        Queue<TreeNode> parentLay = new LinkedList<>();
        parentLay.add(root);
        Queue<TreeNode> sonLay = new LinkedList<>();
        while (!parentLay.isEmpty()) {
            // 求总和
            int sum = 0;
            for (TreeNode current : parentLay) {
                if (current.left != null) {
                    sonLay.add(current.left);
                    sum += current.left.val;
                }
                if (current.right != null) {
                    sonLay.add(current.right);
                    sum += current.right.val;
                }
            }
            // 赋新值
            while (!parentLay.isEmpty()) {
                TreeNode current = parentLay.poll();
                int sonSum = (current.left == null ? 0 : current.left.val)
                        + (current.right == null ? 0 : current.right.val);
                if (current.left != null) {
                    current.left.val = sum - sonSum;
                }
                if (current.right != null) {
                    current.right.val = sum - sonSum;
                }
            }
            Queue<TreeNode> temp = parentLay;
            parentLay = sonLay;
            sonLay = temp;
        }
        return root;
    }
}

c++实现

class Solution {
public:
    TreeNode* replaceValueInTree(TreeNode* root) {
        root->val = 0;
        vector<TreeNode*> parent_lay;
        vector<TreeNode*> son_lay;
        parent_lay.push_back(root);
        while (!parent_lay.empty()) {
            // 求总和
            int sum = 0;
            for (TreeNode* current : parent_lay) {
                if (current->left != nullptr) {
                    son_lay.push_back(current->left);
                    sum += current->left->val;
                }
                if (current->right != nullptr) {
                    son_lay.push_back(current->right);
                    sum += current->right->val;
                }
            }
			// 赋新值
            for (TreeNode* current : parent_lay) {
                int sonSum = (current->left == nullptr ? 0 : current->left->val)
                    + (current->right == nullptr ? 0 : current->right->val);
                if (current->left != nullptr) {
                    current->left->val = sum - sonSum;
                }
                if (current->right != nullptr) {
                    current->right->val = sum - sonSum;
                }
            }
            vector<TreeNode*> temp = parent_lay;
            parent_lay = son_lay;
            son_lay = temp;
            son_lay.clear();
        }
        return root;

    }
}
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值