101. Symmetric Tree(二叉树、五星、递归)

原创 2018年04月17日 20:13:07

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

Note:

Bonus points if you could solve it both recursively and iteratively.


高票答案lvlolitte

递归做法:

public boolean isSymmetric(TreeNode root) {
    return root==null || isSymmetricHelp(root.left, root.right);
}

private boolean isSymmetricHelp(TreeNode left, TreeNode right){
    if(left==null || right==null)//非常机智地同时处理了left,right同时为null和不同时为null的情况
        return left==right;
    if(left.val!=right.val)
        return false;
    return isSymmetricHelp(left.left, right.right) && isSymmetricHelp(left.right, right.left);
}

非递归做法:用到了stack是Vector的一个子类,它实现了一个标准的后进先出的栈。除了继承自vector的方法,自己只有五个方法。下列代码中需注意入栈是先左后右,出栈则先右后左~因为stack后进先出~

1boolean empty() 
测试堆栈是否为空。
2Object peek( )
查看堆栈顶部的对象,但不从堆栈中移除它。
3Object pop( )
移除堆栈顶部的对象,并作为此函数的值返回该对象。
4Object push(Object element)
把项压入堆栈顶部。
5int search(Object element)
返回对象在堆栈中的位置,以 1 为基数。
1boolean empty() 
测试堆栈是否为空。
2Object peek( )
查看堆栈顶部的对象,但不从堆栈中移除它。
3Object pop( )
移除堆栈顶部的对象,并作为此函数的值返回该对象。
4Object push(Object element)
把项压入堆栈顶部。
5int search(Object element)
返回对象在堆栈中的位置,以 1 为基数。

public boolean isSymmetric(TreeNode root) {
    if(root==null)  return true;
    
    Stack<TreeNode> stack = new Stack<TreeNode>();
    TreeNode left, right;
    if(root.left!=null){
        if(root.right==null) return false;
        stack.push(root.left);
        stack.push(root.right);
    }
    else if(root.right!=null){
        return false;
    }
        
    while(!stack.empty()){
        if(stack.size()%2!=0)   return false;
        right = stack.pop();
        left = stack.pop();
        if(right.val!=left.val) return false;
        
        if(left.left!=null){
            if(right.right==null)   return false;
            stack.push(left.left);
            stack.push(right.right);
        }
        else if(right.right!=null){
            return false;
        }
            
        if(left.right!=null){
            if(right.left==null)   return false;
            stack.push(left.right);
            stack.push(right.left);
        }
        else if(right.left!=null){
            return false;
        }
    }
    
    return true;
}

【LeetCode】101. Symmetric Tree 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51471280Subject 出处:https://leetcode.com/pro...
  • crazy1235
  • crazy1235
  • 2016-05-30 23:06:35
  • 6855

LeetCode(101)Symmetric Tree

题目如下: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center)....
  • feliciafay
  • feliciafay
  • 2014-01-17 06:48:54
  • 3072

101. Symmetric Tree [easy] (Python)

题目链接https://leetcode.com/problems/symmetric-tree/题目原文 Given a binary tree, check whether it is a m...
  • coder_orz
  • coder_orz
  • 2016-06-03 15:53:34
  • 1726

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

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

【leetcode c++】101 Symmetric Tree

Symmetric Tree Given a binary tree, check whether it is amirror of itself (ie, symmetric around its...
  • hqq39
  • hqq39
  • 2015-07-27 14:39:02
  • 356

LeetCode101——Symmetric Tree

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). Fo...
  • booirror
  • booirror
  • 2015-02-05 15:33:35
  • 720

leetcode_question_101 Symmetric Tree

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For ...
  • doc_sgl
  • doc_sgl
  • 2013-09-23 00:36:59
  • 2966

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:34
  • 278

【LeetCode-101】 Symmetric Tree(C++)

题目要求:判断一棵二叉树是不是轴对称。 解题方法:递归的方法(用循环实现现在不想写,有点困,等回头写了之后再来改博客) /** * Definition for a binary tree no...
  • liujiayu1015
  • liujiayu1015
  • 2016-09-03 16:34:52
  • 219

101. Symmetric Tree Leetcode Python

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). Fo...
  • hyperbolechi
  • hyperbolechi
  • 2015-01-30 07:28:30
  • 1625
收藏助手
不良信息举报
您举报文章:101. Symmetric Tree(二叉树、五星、递归)
举报原因:
原因补充:

(最多只允许输入30个字)