LeetCode(面试28 对称的二叉树)

如题在这里插入图片描述
一眼看到题目,感觉可以镜像遍历,然后比较结果

public boolean isSymmetric(TreeNode root) {
		if (root == null) {
			return true;
		}
		List<Integer> l1 = new ArrayList<Integer>();
		List<Integer> l2 = new ArrayList<Integer>();
		leftTraversal(l1, root);
		rightTraversal(l2, root);
		return l1.equals(l2);
	}
	
	//先遍历左子树
	private void leftTraversal(List<Integer> li, TreeNode root) {
		li.add(root.val);
		if (root.left != null && root.right != null) {
			leftTraversal(li, root.left);
			leftTraversal(li, root.right);
		} else if (root.left == null && root.right != null) {
			li.add(null);
			leftTraversal(li, root.right);
		} else if (root.left != null && root.right == null) {
			leftTraversal(li, root.left);
			li.add(null);
		}

	}
	
	//先遍历右子树
	private void rightTraversal(List<Integer> li, TreeNode root) {
		li.add(root.val);
		if (root.left != null && root.right != null) {
			rightTraversal(li, root.right);
			rightTraversal(li, root.left);
		} else if (root.left == null && root.right != null) {
			rightTraversal(li, root.right);
			li.add(null);
		} else if (root.left != null && root.right == null) {
			li.add(null);
			rightTraversal(li, root.left);
		}
	}

结果在这里插入图片描述
很显然又优化余地,左右镜像只用比较左右子树即可

public boolean isSymmetric1(TreeNode root) {
		if (root == null || (root.left == null && root.right == null)) {
			return true;
		} else if (root.left == null || root.right == null) {
			return false;
		}
		List<Integer> l1 = new ArrayList<Integer>();
		List<Integer> l2 = new ArrayList<Integer>();
		leftTraversal(l1, root.left);
		rightTraversal(l2, root.right);
		return l1.equals(l2);
	}

额,效率上并没有什么进步。那就换个思路,直接递归

public boolean isSymmetric2(TreeNode root) {
		return root == null ? true : res(root.left, root.right);
	}

	private boolean res(TreeNode left, TreeNode right) {
		if (left == null && right == null) {
			return true;
		}
		if (left == null || right == null || left.val != right.val) {
			return false;
		}
		return res(left.left, right.right) && res(left.right, right.left);
	}

在这里插入图片描述
效率大大提升,但是总感觉还是有优化余地,是否可以匹配到不等直接退出

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值