LeetCode - Easy - 101. Symmetric Tree

本文介绍了一种判断二叉树是否对称的方法,通过广度优先搜索(BFS)和深度优先搜索(DFS)两种方式实现。提供了详细的Java代码示例,并通过单元测试验证了算法的有效性。

Topic

  • Tree
  • Depth-First Search
  • Breadth-first Search

Description

https://leetcode.com/problems/symmetric-tree/

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree [1,2,2,3,4,4,3] is symmetric:

    1
   / \
  2   2
 / \ / \
3  4 4  3

But the following [1,2,2,null,3,null,3] is not:

    1
   / \
  2   2
   \   \
   3    3

Follow up: Solve it both recursively and iteratively.

Analysis

方法一:BFS

方法二:DFS(递归)

Submission

import java.util.Arrays;
import java.util.LinkedList;

import com.lun.util.BinaryTree.TreeNode;

public class SymmetricTree {

	//方法一:我写的BFS
	public boolean isSymmetric1(TreeNode root) {
		if (root == null) return true;
		LinkedList<TreeNode> queue = new LinkedList<>(Arrays.asList(root.left, root.right));

		while (!queue.isEmpty()) {
			int length = queue.size();

			for (int i = 0, j = length - 1; i < length / 2; i++, j--) {
				TreeNode tn1 = queue.get(i);
				TreeNode tn2 = queue.get(j);

				if (tn1 == null && tn2 != null || tn1 != null && tn2 == null) {
					return false;
				}

				if (tn1 != null && tn2 != null) {
					if (tn1.val != tn2.val) {
						return false;
					}
				}
			}

			for(int i = length - 1; i >= 0; i--) {
				TreeNode node = queue.get(i);
				queue.remove(i);
				if(node != null)
					queue.addAll(i, Arrays.asList(node.left, node.right));
			}
		}
		return true;
	}

	//方法一:别人写的BFS
	public boolean isSymmetric2(TreeNode root) {
		if (root == null) return true;
		LinkedList<TreeNode> q = new LinkedList<>(Arrays.asList(root.left, root.right));
		while (!q.isEmpty()) {
			TreeNode left = q.poll(), right = q.poll();
			if (left == null && right == null) continue;
			if (left == null || right == null || left.val != right.val) return false;
			q.addAll(Arrays.asList(left.left, right.right, left.right, right.left));
		}
		return true;
	}
	
	//方法二:
	public boolean isSymmetric3(TreeNode root) {
		return root == null || isSymmetric3(root, root);
	}
	
	private boolean isSymmetric3(TreeNode root1, TreeNode root2) {
		if(root1 == null && root2 != null || root1 != null && root2 == null)
			return false;
		
		if(root1 != null && root2 != null) {
			
			if(root1.val != root2.val) {
				return false;
			}
			return isSymmetric3(root1.left, root2.right) && isSymmetric3(root1.right, root2.left);
		}
		
		return true;
	}
}

Test

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

import com.lun.util.BinaryTree.TreeNode;

public class SymmetricTreeTest {

	private TreeNode root = null;
	
	@Before
	public void init() {
		root = new TreeNode(1);
		root.left = new TreeNode(2);
		root.right = new TreeNode(2);
	}
	
	
	@Test
	public void test1() {
		SymmetricTree obj = new SymmetricTree();

		root.left.left = new TreeNode(3);
		root.left.right = new TreeNode(4);
		root.right.left = new TreeNode(4);
		root.right.right = new TreeNode(3);
		
		assertTrue(obj.isSymmetric1(root));
		assertTrue(obj.isSymmetric2(root));
		assertTrue(obj.isSymmetric3(root));
	}
	
	@Test
	public void test2() {
		SymmetricTree obj = new SymmetricTree();
		
		root.left.right = new TreeNode(3);
		root.right.right = new TreeNode(3);
		
		assertFalse(obj.isSymmetric1(root));
		assertFalse(obj.isSymmetric2(root));
		assertFalse(obj.isSymmetric3(root));
	}
}
【源码免费下载链接】:https://renmaiwang.cn/s/jmsue 卷积神经网络(Convolutional Neural Networks, CNN)是一种深度学习模型,在图像处理和计算机视觉领域具有重要应用价值。通过MATLAB这一强大的工具平台,我们可以方便地实现CNN模型的构建、训练与优化过程。该压缩包中的MATLAB代码提供了一个完整的CNN实例,用户可以直接运行并观察其工作原理。理解CNN的基本结构是掌握其核心功能的关键。CNN通常由卷积层、池化层、全连接层以及激活函数等主要组件构成。具体来说,卷积层通过使用卷积核对输入图像进行扫描操作,提取图像中的特征信息;池化层则能够有效降低数据维度的同时减少计算量,并保留关键的视觉信息特征;全连接层负责将之前提取的特征信号映射到目标任务(如分类或回归)所需的输出结果空间中。此外,在MATLAB环境下,我们可以通过`deepLearningNetwork`函数轻松创建一个CNN模型架构。具体步骤包括:首先定义网络结构参数,例如卷积层的数量、尺寸以及激活函数类型等;其次设计完整的网络层次结构,并配置相关的超参数设置;最后利用提供的训练数据对模型进行优化和调参。在实际操作中,用户需要准备并整理好适合CNN处理的高质量图像数据集,并对其进行预处理工作,如归一化、裁剪或翻转等;接着可以使用MATLAB内置的数据导入与管理工具(如`imageDatastore`)来简化数据加载流程;最后通过设置合适的训练选项参数和执行训练过程,使模型能够自动学习并提取具有判别性的特征。在模型训练完成后,用户可以通过调用`classify`或`predict`函数对测试集中的图像进行分类预测,并评估模型的性能表现。值得注意的是,在这个压缩包中提供的CNN代码实例可能包含了从数据准备到模型部署的完整流程,其中包括了可视化、超参数调整等功能模
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值