关闭

【Leetcode】Symmetric Tree

285人阅读 评论(0) 收藏 举报
分类:

题目链接: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 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 recursively and iteratively.

思路:

注意我在递归中保存结果的方式,跟Kth Smallest Element in a BST 算法2和Balanced Binary Tree 中是一样的。

基本框架是:

	boolean result = true;
	public boolean recur(TreeNode root){
		if(root!=null){
			if(satisfy a condition){
				result = false;
			}
			recur(root.left);
			recur(root.right);
		}
		return result;
	}


算法:

	public boolean isSym(TreeNode p, TreeNode q) {
		if (p == null && q == null) {//都为空
			return true;
		}else if (p == null || q == null) {//有一个不为空
			result = false;
		}else if (p.val == q.val) {
			isSym(p.left, q.right);//注意对称
			isSym(p.right, q.left);
		} else {
			result = false;
		}
		return result;
	}

	public boolean isSymmetric(TreeNode root) {
		if (root == null) { 
			return true;
		}
		if (root.left == null && root.right == null) { //单结点
			return true;
		}
		if (root.left == null || root.right == null) {//不对称
			return false;
		}
		return isSym(root.left, root.right); 
	}

	boolean result = true;


0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

Leetcode Symmetric Tree 递归和非递归解法

和same tree题目差不多一样的解法。 1 recursively 递归法: 2 iteratively 非递归: 使用先序遍历,原来树的先序遍历的非递归写法也不简单。
  • kenden23
  • kenden23
  • 2014-02-16 01:11
  • 14319

Leetcode-Symmetric Tree——判断二叉树是否对称

题目 Symmetric Tree  Total Accepted: 12814 Total Submissions: 40358My Submissions Given a binary tree, check whether it is a...
  • disappearedgod
  • disappearedgod
  • 2014-04-20 01:00
  • 2060

【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 c...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-07 07:29
  • 3685

关于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 [1,2,2...
  • zsy112371
  • zsy112371
  • 2016-09-03 16:54
  • 299

LeetCode:Symmetric Tree(镜像二叉树)

LeetCode:Symmetric Tree(镜像二叉树)1、题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this b...
  • BestZem
  • BestZem
  • 2016-07-16 11:23
  • 237

LeetCode之Tree题目汇总

Balanced Binary TreeGiven a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tre...
  • Yano_nankai
  • Yano_nankai
  • 2015-12-29 20:43
  • 696

【leetcode 二叉树对称判断】Symmetric Tree

1、题目 2、分析 3、daim
  • u012162613
  • u012162613
  • 2014-11-17 18:03
  • 2266

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 symmetri...
  • lqcsp
  • lqcsp
  • 2014-04-15 05:25
  • 895

和大神们学习每天一题(leetcode)-Symmetric Tree

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
  • majunyangyang
  • majunyangyang
  • 2014-11-19 08:59
  • 178

LeetCode题解:Symmetric Tree(有4种解法)

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 /...
  • u012403246
  • u012403246
  • 2014-11-17 19:01
  • 606
    个人资料
    • 访问:557900次
    • 积分:8566
    • 等级:
    • 排名:第2662名
    • 原创:305篇
    • 转载:6篇
    • 译文:0篇
    • 评论:37条
    博客专栏
    文章分类
    最新评论