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

算法分析:和same tree比较像,都可以用递归方法来解决。

public class SymmeticTree
{
	public boolean isSymmetric(TreeNode root)
	{
		if (root == null)
		{
			return true;
		}
		return isSymmetric(root.left, root.right);
	}

	public boolean isSymmetric(TreeNode left, TreeNode right)
	{
		if (left == null && right == null)
		{
			return true;
		}
		else if ((left == null && right != null)
				|| (right == null && left != null) || (left.val != right.val))
		{
			return false;
		}
		else
		{
			return isSymmetric(left.left, right.right)
					&& isSymmetric(left.right, right.left);
		}
	}
}
时间: 2024-08-04 18:45:32

Symmetric Tree,对称树的相关文章

leetCode 101. Symmetric Tree 对称树

101. 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]

leetcode 之 Symmetric Tree 镜像树

Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not: 1 / 2 2 \ 3 3 递归解法:由于对称性,每次把一个节点的左子树和它兄弟节点的左子树进行

leetcode-Symmetric Tree 对称树

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not: 1 / 2 2 \ 3 3 方法一: 层次遍历是最直观的方法.对数进行层次遍历,记录每一层的节点,然后对每一层的value组成

[Leetcode] Symmetric tree 对称二叉树

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not: 1 / 2 2 \ 3 3 Note: Bonus points if you could solve it both rec

【LeetCode】Symmetric Tree 判断一棵树是否是镜像的

题目:Symmetric Tree <span style="font-size:18px;">/**LeetCode Symmetric Tree 对称的树 * 思路:判断一棵树是否对称,1.有左子树就要有右子树 * 2.除根节点外对称节点值要相同 * 注意:对称后就是左子树的左节点和右子树的右节点比较 * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; *

对称树 Symmetric Tree

算法能力一直不好,决定利用晚上和假期时间多做点算法题,动动手,提高自己.大家看到我的水平低还望莫要嘲笑,嘻嘻. 这一题呢是LeetCode上的对称树问题, Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the fol

Leetcode:Symmetric Tree 判断对称树

Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not: 1 / 2 2 \ 3 3 解题分析: 二叉树递归,始终是第一颗二叉树的左子树和第二颗二叉树的右

LeetCode:Symmetric Tree - 判断二叉树是否对称

1.题目名称 Symmetric Tree(判断二叉树是否对称) 2.题目地址 https://leetcode.com/problems/symmetric-tree/ 3.题目内容 英文:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). 中文:给定一颗二叉树,检查它是否与自己的镜像是同一棵树(即围绕根节点对称). 4.解题方法 本题与题目"Same Tr

Leetcode 树 Symmetric Tree

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Symmetric Tree Total Accepted: 13991 Total Submissions: 44240 Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmet

【LeetCode-面试算法经典-Java实现】【101-Symmetric Tree(对称树)】

[101-Symmetric Tree(对称树)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not: