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());
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,424評(píng)論 0 10
  • 我發(fā)現(xiàn)自己太容易對(duì)食物失去控制,尤其是是對(duì)面包、蛋糕和酸奶。不明所以,蛋糕和酸奶總是帶給我一種幸福的感覺,但現(xiàn)在有...
    桑尼冉閱讀 255評(píng)論 0 1
  • 我不優(yōu)秀, 但我善良不虛偽, 我有話直來(lái)直去! 做事坦坦蕩蕩! 我不聰明,但我肯定不傻, 很多事,我都能看明白, ...
    EASON陳俊道閱讀 565評(píng)論 0 0
  • 文/聽說(shuō)名字要長(zhǎng)才霸氣 說(shuō)實(shí)話,小編喜歡的明星不多,總覺得娛樂(lè)圈的人們蒙了層面紗,明明一副好男人形象總爆出劈腿這...
    魔怔君閱讀 261評(píng)論 0 0
  • 感賞日記第三十三篇7月14日 你喜歡奮斗,方法就越來(lái)越多;你喜歡放棄,借口就越來(lái)越多;你喜歡感恩,順利就越多!你喜...
    梓萱萱萱萱閱讀 183評(píng)論 0 1