Wireless Sensor Networks (WSNs) face challenges related to limited energy resources in sensor nodes, making network lifetime and energy consumption critical considerations. In this paper, we introduce a novel approach to extend network lifetime and reduce energy consumption in WSNs by optimizing network architecture selection. We explore various architecture options, including single-layer and multi-layer networks, to determine the most suitable configuration. Several studies have applied knowledge transfer in evolutionary multitasking optimization to maximize network lifetime, achieving state-of-the-art results for the problem. However, they still have some drawbacks, such as redundant representations, i.e., many genotypes represent the same phenotype, and invalid solution generation. To address these limitations, we propose a new evolutionary multitasking algorithm incorporating efficient encoding to represent tree-structure solutions (network architectures) to the problem. Additionally, we tailor new genetic operators, such as recombination and mutation, to meet the specific requirements of our proposed evolutionary multitasking algorithm. The standout feature of our proposal lies in its ability to consistently generate valid solutions for the problem, which significantly reduces redundancy within the genotype search space and effectively satisfies the problem constraints. Additionally, our multitask evolutionary algorithm facilitates the exploration of various network architectures, harnessing knowledge transfer to enhance optimal performance and reduce computation time compared to single-task methods. We conduct comprehensive experiments on diverse datasets and use statistical tests, e.g., the Wilcoxon signed rank test, to verify the performance of our proposed algorithms. The empirical results demonstrate that our proposed algorithm outperforms existing methods and achieves state-of-the-art results in terms of solution quality, convergence rate, and running time across a wide range of data instances. Specifically, our algorithm provides 49.54% better solution quality on average across all data types compared to the previous state-of-the-art method.