100 Same Tree


title: Same Tree
tags:
- same-tree
- No.100
- simple
- tree
- recurrence
- depth-first-search


Description

Given two binary trees, write a function to check if they are the same or not.

Two binary trees are considered the same if they are structurally identical and the nodes have the same value.

Example 1:

Input:     1         1
          / \       / \
         2   3     2   3

        [1,2,3],   [1,2,3]

Output: true

Example 2:

Input:     1         1
          /           \
         2             2

        [1,2],     [1,null,2]

Output: false

Example 3:

Input:     1         1
          / \       / \
         2   1     1   2

        [1,2,1],   [1,1,2]

Output: false

Corner Cases

  • two empty trees
  • one empty tree
  • null node

Solutions

Pre-Order Depth First Search

Take depth first search to compare sub-trees. Use pre-order because we can stop the program as soon as an inequal case is detected.

For any tree node, if the height of the left sub-tree is h_L and the height of the right one is h_R, then we have:

T(\max\{h_L, h_R\} + 1) = T(h_L) + T(h_R) + O(1)

Since we visit each edge and vertex once, then running time is O(V + E).

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        return isSameSubTree(p, q);
    }
    
    private boolean isSameSubTree(TreeNode sp, TreeNode sq) {
        if (sp == null && sq == null) {return true;}
        if (sp == null || sq == null) {return false;}
        else if (sp.val != sq.val)    {return false;}
        
        boolean leftSame  = isSameSubTree(sp.left, sq.left);
        boolean rightSame = isSameSubTree(sp.right, sq.right);
        return leftSame && rightSame;
    }
}

Stack Without Recurrence

Use stack to implement pre-order DFS and compare the two trees.

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        Stack<TreeNode> sp = new Stack<TreeNode>();
        Stack<TreeNode> sq = new Stack<TreeNode>();

        TreeNode xp = p;
        TreeNode xq = q;

        if (p == null && q != null) {return false;}

        while (xp != null || !sp.empty()) {
            if (xp != null) {
                if (xq == null)       {return false;}
                if (xp.val != xq.val) {return false;}
                sp.push(xp);
                sq.push(xq);
                xp = xp.left;
                xq = xq.left;
            } else { //xp == null && !sp.empty()
                if (xq != null) {return false;}
                TreeNode tp = sp.pop();
                TreeNode tq = sq.pop();
                xp = tp.right;
                xq = tq.right;
            }
        }
        return true;
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,990评论 0 10
  • 爸爸出差了。 从计划到出行足足有两个月。 这两个月里,妈妈一直都处于焦虑的状态,吃不好睡不好,担心爸爸走了之后咱们...
    江小馨阅读 420评论 3 3
  • 时间7.7号 车架号0450623 045076 时间7.12 车架号050651 时间7.12 车架号05028...
    丿肆悦阅读 266评论 0 0
  • 最近又陷入了日常丧。 回来的前一天喉咙被鸭脆骨卡了喉咙,呕吐了好久,但嗓子里依然有种怪怪的感觉,不是卡,但感觉有什...
    树风水阅读 376评论 0 0
  • 遇到一个同事,一直感慨自己的孩子日记写不来,不知道该写些什么,她说,火来就让这孩子每天都写一篇日记,随便她写什么,...
    一尘720阅读 168评论 4 2

友情链接更多精彩内容