zoukankan      html  css  js  c++  java
  • [Leetcode 6] 100 Same Tree

    Problem:

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

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

    Analysis:

    Use a queue to do level order traversal is not proper in this problem since this method ignores the structure information of the tree. One possible way to deal with this problem is recursion. Starting from the root, 1. check the existance of nodes, if both are null, return true; One is null, the other is not, return false; 2. check the value equivalence, if not equal, return false, else recursively check their left and right children's equivalence.

    Then time complexity in worst case is O(2*min{m, n}) where we have to check all the nodes of smaller tree and corresponding nodes in the larger tree. The space complexity is O(n+m), where n is the size of the first tree and m is the size of the second tree.

    Code:

     1 /**
     2  * Definition for binary tree
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode(int x) { val = x; }
     8  * }
     9  */
    10 public class Solution {
    11     public boolean isSameTree(TreeNode p, TreeNode q) {
    12         // Start typing your Java solution below
    13         // DO NOT write main() function
    14 
    15          if (p==null && q==null) 
    16               return true;
    17           else if ((p==null && q!=null) || (p!=null && q==null))
    18               return false;
    19           else if (p.val != q.val) 
    20               return false;
    21           else
    22               return (isSameTree(p.left, q.left)) 
    23                       && (isSameTree(p.right, q.right));
    24     }
    25 }

    Attention:

    Here is the first wrong implementation of this problem:

     1 /**
     2  * Definition for binary tree
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode(int x) { val = x; }
     8  * }
     9  */
    10 public class Solution {
    11     public boolean isSameTree(TreeNode p, TreeNode q) {
    12         // Start typing your Java solution below
    13         // DO NOT write main() function
    14 
    15         if (p==null && q==null) 
    16             return true;
    17         else if ((p==null && q!=null) || 
    18                     (q!=null && q==null)) 
    19             return false;
    20         else if (p.val != q.val) 
    21             return false;
    22         else
    23             return (isSameTree(p.left, q.left)) 
    24                     && (isSameTree(p.right, q.right));
    25     }
    26 }

    There is a typo in line 18, where the condition should be (p!=null && q==null), be careful while typing.

    Another thing is be careful with the comparision with null, it means that the given reference points to nothing, not the regerenced value is null. To check references value is equivalent or not, use ref1.equals(ref2) rather than ref1==ref2.

  • 相关阅读:
    How to Downgrade From iPhone Firmware 2.2 to 2.1 视频教程降级iphone 2.2 到2.1
    11种网站测试软件(转) 沧海一粟
    GoF23种设计模式之行为型模式之观察者模式
    Android柳叶刀之Button之图文并茂
    GoF23种设计模式之行为型模式之迭代器模式
    某大型银行深化系统之十九:日志规范
    GoF23种设计模式之行为型模式之中介者模式
    GoF23种设计模式之行为型模式之备忘录模式
    Android血刀之CheckBox之问卷调查
    Android应用开发之MetaData之数据挖掘
  • 原文地址:https://www.cnblogs.com/freeneng/p/3003335.html
Copyright © 2011-2022 走看看