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.

題意:

給定兩棵二叉樹,寫一個方法來確定他們是否一樣(兩棵樹的結構與樹中的每個節點都要一樣)。

題解:

對兩棵樹進行歷遍,在歷遍的過程中

  1. 到兩棵樹的最末節點下面(p,q都是null),則返回true
  2. 當左為null,右非null,此樹不相同,返回false
  3. 當左非null,右為null,此樹不相同,返回false
  4. 當兩邊的節點值不一樣,此樹不相同,返回false
  5. 遞歸訪問左子樹
  6. 遞歸訪問右子樹
最後返回結果

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) {
    	return helper(p, q);
    }
    
    boolean helper(TreeNode p, TreeNode q) {
    	//到最末節點的下面,則返回true
        if(p == null && q == null)
            return true;
        
        // 左沒有,右有,此樹不相同             左有,右沒有,此樹不相同
        if((p == null && q != null) || (p != null && q == null))
            return false;
        
        //兩邊的節點值不一樣,此樹不相同
        if(p.val != q.val)
            return false;
        
        boolean isSame = true;
        
        //歷遍左邊子樹
        isSame &= helper(p.left, q.left);
        //歷遍右邊子樹
        isSame &= helper(p.right, q.right);
        
        return isSame;
    }
}
在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)来比较两个模型的优劣。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值