100. Same Tree

本文介绍了一个用于检查两棵二叉树是否相同的算法。通过递归地比较每棵树的节点值来确定它们是否结构一致且节点值相等。

Given two binary trees, write a function to check if they are equal or not.

Two binary trees are considered equal if they are structurally identical and the nodes have the same value.

題意:給定兩顆二叉樹,寫一個方法來判斷它們是否相同

題解:

遞歸地歷遍左右子樹,遇到不同的就返回false,樹不同有以下三種情況:

  1. 左有節點,右沒有節點
  2. 左沒有節點,右有節點
  3. 兩邊的節點值不一樣
遇到上面三種情況則直接返回false,而歷遍到最末節點以下(兩個節點都為null),則返回true,表示從上面到此最末節點都是一樣的


package LeetCode.Easy;

import LeetCode.Dependencies.TreeNode;

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class SameTree {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        //到最末節點的下面,則返回true
        if(p == null && q == null)
            return true;
        
        //左有,右沒有,此樹不相同
        if(p == null && q != null)
            return false;
        //左沒有,右有,此樹不相同
        if(p != null && q == null)
            return false;
        
        //兩邊的節點值不一樣,此樹不相同
        if(p.val != q.val)
            return false;
        
        //歷遍右邊子樹
        boolean isRightSame = isSameTree(p.right, q.right);
        if(isRightSame == false)
            return false;
        
        //歷遍左邊子樹
        boolean isLeftSame = isSameTree(p.left, q.left);
        if(isLeftSame == false)
            return false;
        
        //若都經過以上的關卡都沒有返回false,這表示兩棵樹都是一樣的
        return true;
    }
}


在R语言中,可以按照以下步骤加载`AmesHousing`包,设置相同超参数拟合随机森林和袋装树模型,并比较它们的优劣。 首先,确保已经安装了所需的包,这里需要`AmesHousing`、`randomForest`和`ipred`包。 ```R # 安装并加载所需的包 if (!require(AmesHousing)) { install.packages("AmesHousing") library(AmesHousing) } if (!require(randomForest)) { install.packages("randomForest") library(randomForest) } if (!require(ipred)) { install.packages("ipred") library(ipred) } # 加载数据集 data("ames_raw") # 数据预处理 # 去除含有缺失值的行 ames_data <- na.omit(ames_raw) # 提取特征和目标变量 X <- ames_data[, -which(names(ames_data) == "Sale_Price")] y <- ames_data$Sale_Price # 设置超参数 ntree <- 500 mtry <- ncol(X) # 对于袋装树,mtry等于特征数量 # 拟合随机森林模型 rf_model <- randomForest(x = X, y = y, ntree = ntree, mtry = mtry) # 拟合袋装树模型 bagging_model <- bagging(formula = Sale_Price ~ ., data = ames_data, nbagg = ntree) # 评估模型 # 划分训练集和测试集 set.seed(123) train_index <- sample(1:nrow(ames_data), 0.7 * nrow(ames_data)) train_data <- ames_data[train_index, ] test_data <- ames_data[-train_index, ] # 重新拟合模型 rf_model_train <- randomForest(x = train_data[, -which(names(train_data) == "Sale_Price")], y = train_data$Sale_Price, ntree = ntree, mtry = mtry) bagging_model_train <- bagging(formula = Sale_Price ~ ., data = train_data, nbagg = ntree) # 在测试集上进行预测 rf_pred <- predict(rf_model_train, newdata = test_data) bagging_pred <- predict(bagging_model_train, newdata = test_data) # 计算均方误差(MSE) rf_mse <- mean((test_data$Sale_Price - rf_pred)^2) bagging_mse <- mean((test_data$Sale_Price - bagging_pred)^2) # 比较模型 if (rf_mse < bagging_mse) { print("随机森林模型表现更好") } else if (rf_mse > bagging_mse) { print("袋装树模型表现更好") } else { print("两个模型表现相同") } ``` 在上述代码中,首先加载了`AmesHousing`包并获取数据集,然后进行数据预处理,接着设置了相同的超参数(树的数量`ntree`)来拟合随机森林和袋装树模型。之后划分训练集和测试集,重新在训练集上拟合模型并在测试集上进行预测,最后通过计算均方误差(MSE)来比较两个模型的优劣。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值