关闭

101. Symmetric Tree

104人阅读 评论(0) 收藏 举报
分类:
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

这里是判断二叉树是否对称,如果用递归来做一目了然,相当于同时遍历左右两个子数,要求相互对称。传递的两个根节点需要同为NULL或者值相等,然后要求二者左右、右左的下一级子树同时也对称。代码如下:

方法一:递归

bool isSymmetric(TreeNode* root) {
    if (root == NULL) return true;
    return helper(root->left, root->right);
}
bool helper(TreeNode* p, TreeNode *q) {
    if (p == NULL || q == NULL)
    return (p == NULL && q == NULL);
    if (p->val != q->val) return false;
    if (!helper(p->left, q->right)) return false;
    return helper(p->right, q->left);
}

方法二:迭代法

其实还可以像堆栈实现前序遍历那样来做类似操作。具体实现如下:

bool isSymmetric(TreeNode* root) {
    if (root == NULL) return true;
    stack<TreeNode*> s1, s2;
    s1.push(root->left), s2.push(root->right);
    while (!s1.empty() && !s2.empty()) {
        TreeNode *temp1 = s1.top();
        TreeNode *temp2 = s2.top();
        s1.pop();//pop返回值为void,必须先top再pop
        s2.pop();
        if ((!temp1 && temp2) || (temp1 && !temp2)) return false;
        if (temp1 && temp2) {
            if (temp1->val != temp2->val) return false;
            s1.push(temp1->left);
            s1.push(temp1->right);
            s2.push(temp2->right);
            s2.push(temp2->left);
        }
    }
    return true;
}

相比之下,递归更好实现,更容易理解,递归只用处理好局部问题就行,每一次的判断都是对局部最终结果的直接判断,所以在递归里两个都是NULL可以直接返回true,但是在迭代里不行,迭代里出现错误可以直接返回错,因为局部错整体一定错,但是局部对了,仍然要对其他局部做判断。

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

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

【101-Symmetric Tree(对称树)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given a binary tree, check whether it...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-07 07:29
  • 3659

Leetcode Symmetric Tree 递归和非递归解法

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

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

题目 Symmetric Tree  Total Accepted: 12814 Total Submissions: 40358My Submissions Given a bina...
  • disappearedgod
  • disappearedgod
  • 2014-04-20 01:00
  • 2052

101. Symmetric Tree 对称树 (难点!)

https://leetcode.com/problems/symmetric-tree/ Given a binary tree, check whether it is a mirror ...
  • gao1440156051
  • gao1440156051
  • 2016-06-25 19:14
  • 256

101. Symmetric Tree | 判断二叉树是否为镜像二叉树

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For ...
  • Yangtong157
  • Yangtong157
  • 2017-02-24 16:05
  • 97

【LeetCode】101. Symmetric Tree 中序遍历,分支遍历,二叉树

101. Symmetric Tree Total Accepted: 103742 Total Submissions: 306773 Difficulty: Easy Gi...
  • Jin_Kwok
  • Jin_Kwok
  • 2016-04-15 16:21
  • 501

leetcode -- 101. Symmetric Tree 【对称树,结构,内容】

题目 Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F...
  • TheSnowBoy_2
  • TheSnowBoy_2
  • 2017-02-11 10:17
  • 581

Symmetric Tree(中心对称树)

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For ...
  • cow__sky
  • cow__sky
  • 2014-03-07 21:37
  • 838

Symmetric Tree(对称树)

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).(给定一棵二...
  • ajiangfan
  • ajiangfan
  • 2016-11-09 16:44
  • 117

关于LeetCode中Symmetric Tree一题的理解

题目如下: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its cen...
  • zsy112371
  • zsy112371
  • 2016-09-03 16:54
  • 299
    个人资料
    • 访问:23148次
    • 积分:1758
    • 等级:
    • 排名:千里之外
    • 原创:148篇
    • 转载:4篇
    • 译文:1篇
    • 评论:2条
    文章分类
    最新评论