101 Symmetric Tree


title: Symmetric Tree
tags:
- symmetric-tree
- No.101
- simple
- tree
- depth-first-search
- recurrence
- stack


Description

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.

Corner Cases

  • empty tree
  • single root

Solutions

Pre-Order Depth First Search

For root node, we have two sub-trees:

     +----- root -----+
     |                |
Left Sub-Tree   Right Sub-Tree

Use pre-order dfs to visit the two sub-trees and return false as long as the nodes in them are inequivalent. Compare left with right and right with left.

The running time is O(V+E), same as dfs in graph.

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isSymmetric(TreeNode root) {
        return (root == null) ? true : isSymmetricSubTree(root.left, root.right);
    }
    
    private boolean isSymmetricSubTree(TreeNode p, TreeNode q) {
        if (p == null && q == null) {return true;}
        if (p == null || q == null) {return false;}
        else if (p.val != q.val)    {return false;}
        
        boolean leftRight = isSymmetricSubTree(p.left, q.right);
        boolean rightLeft = isSymmetricSubTree(p.right, q.left);
        return leftRight && rightLeft;
    }
}

Stack Without Recurrence

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isSymmetric(TreeNode root) {
        if (root == null) {return true;}
        
        Stack<TreeNode> sl = new Stack<TreeNode>();
        Stack<TreeNode> sr = new Stack<TreeNode>();
        
        TreeNode xl = root.left;
        TreeNode xr = root.right;
        
        if (xl == null && xr != null) {return false;}
        
        while(xl != null || !sl.empty()) {
            if (xl != null) {
                if (xr == null)       {return false;}
                if (xl.val != xr.val) {return false;}
                sl.push(xl);
                sr.push(xr);
                xl = xl.left;
                xr = xr.right;
            }
            else {
                if (xr != null) {return false;}
                xl = sl.pop();
                xr = sr.pop();
                xl = xl.right;
                xr = xr.left;
            }
        }        
        return !(xr != null || !sr.empty());
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,854评论 0 10
  • 我发现自己太容易对食物失去控制,尤其是是对面包、蛋糕和酸奶。不明所以,蛋糕和酸奶总是带给我一种幸福的感觉,但现在有...
    桑尼冉阅读 304评论 0 1
  • 我不优秀, 但我善良不虚伪, 我有话直来直去! 做事坦坦荡荡! 我不聪明,但我肯定不傻, 很多事,我都能看明白, ...
    EASON陈俊道阅读 609评论 0 0
  • 文/听说名字要长才霸气 说实话,小编喜欢的明星不多,总觉得娱乐圈的人们蒙了层面纱,明明一副好男人形象总爆出劈腿这...
    魔怔君阅读 293评论 0 0
  • 感赏日记第三十三篇7月14日 你喜欢奋斗,方法就越来越多;你喜欢放弃,借口就越来越多;你喜欢感恩,顺利就越多!你喜...
    梓萱萱萱萱阅读 230评论 0 1

友情链接更多精彩内容