In wireless sensor networks (WSNs), achieving energy efficiency, security, and minimizing route change propagation time is essential for maintaining optimal performance. This paper introduces a new approach that combines Bray Jaccard Curtis-based Calinski Harabasz k-means (BJC-CHKMeans) for clustering and Karl Pearson correlation-based egret swarm optimization algorithm (KPC-ESOA) for selecting the best cluster head (CH) and path, along with classifying long short-term memory with gated recurrent units (CLE-GRU) for detecting harmful nodes. The methodology aims to enhance energy usage, improve routing efficiency, and strengthen security by identifying malicious nodes. Additionally, it integrates a secure routing table using elbow de-swinging k-anonymity (EDS-KA) and employs digital signature algorithm-based Zeta Bernoulli Merkle tree (DSA-ZBMT) to ensure secure communication with sink nodes. The WSN-DS dataset was used for training and testing, with rigorous preprocessing, feature extraction, and selection to maintain data integrity. Experimental results revealed that the proposed BJC-CHKMeans and CLE-GRU models outperform traditional methods in power consumption, latency, and accuracy. The system achieved a power consumption of 2.1 mW for clustering and 1.9 mW for classification, while also providing near-perfect accuracy in detecting harmful nodes. These findings demonstrate that the framework significantly enhances the energy efficiency and security of WSNs, making it a highly effective solution for large, dynamic sensor networks.