zoukankan      html  css  js  c++  java
  • LeetCode 572. Subtree of Another Tree

    原题链接在这里:https://leetcode.com/problems/subtree-of-another-tree/#/description

    题目:

    Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and node values with a subtree of s. A subtree of s is a tree consists of a node in s and all of this node's descendants. The tree s could also be considered as a subtree of itself.

    Example 1:
    Given tree s:

         3
        / 
       4   5
      / 
     1   2
    

    Given tree t:

       4 
      / 
     1   2
    

    Return true, because t has the same structure and node values with a subtree of s.

    Example 2:
    Given tree s:

         3
        / 
       4   5
      / 
     1   2
        /
       0
    

    Given tree t:

       4
      / 
     1   2
    

    Return false.

    题解:

    Traverse s 的每个节点,看把该节点当成root的subtree是否与t identical.

    如果s 和 t本身都是null, 那么t也算s的subtree, 因为null 是本身null 的subtree.

    Time Complexity: O(m*n). m是s的节点数. n是t的节点数. 对于s的每一个节点都做了一次以该节点为root的subtree 与 t的比较.

    Space: O(logm). stack space, 一直找不到的时候会走到s的底部.

    AC Java:

     1 /**
     2  * Definition for a binary tree node.
     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 isSubtree(TreeNode s, TreeNode t) {
    12         if(isSame(s, t)){
    13             return true;
    14         }
    15         return s!=null && (isSubtree(s.left, t) || isSubtree(s.right,t ));
    16     }
    17     
    18     private boolean isSame(TreeNode s, TreeNode t){
    19         if(s == null && t == null){
    20             return true;
    21         }
    22         
    23         if(s == null || t == null){
    24             return false;
    25         }
    26         
    27         if(s.val != t.val){
    28             return false;
    29         }
    30         
    31         return isSame(s.left, t.left) && isSame(s.right, t.right);
    32     }
    33 }

    对s 和 t 分别做preorder traversal, 然后看t得到的string 是不是 s得到string的 substring.

    遇到null TreeNode时要添加特别符号, 不然就不能区分 123, null, null 和 1, 2, 3. 也无法区分 1, null, 2 和 1, 2, null. 这两种情况.

    Time Complexity: O(m+n). m是s的节点数. n是t的节点数.

    Space: O(Math.max(logm, logn)). Stack space.

    AC Java:

     1 /**
     2  * Definition for a binary tree node.
     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 isSubtree(TreeNode s, TreeNode t) {
    12         String sPreorder = preorderTraversal(s);
    13         String tPreorder = preorderTraversal(t);
    14         
    15         return sPreorder.contains(tPreorder);
    16     }
    17     
    18     private String preorderTraversal(TreeNode root){
    19         StringBuilder sb = new StringBuilder();
    20         Stack<TreeNode> stk = new Stack<TreeNode>();
    21         stk.push(root);
    22         while(!stk.isEmpty()){
    23             TreeNode tn = stk.pop();
    24             if(tn == null){
    25                 sb.append(",#");
    26             }else{
    27                 sb.append(","+tn.val);
    28                 stk.push(tn.left);
    29                 stk.push(tn.right);
    30             }
    31         }
    32         return sb.toString();
    33     }
    34 }

    类似Same TreeBinary Tree Preorder Traversal.

  • 相关阅读:
    列表、元组、字典、集合类型及其内置方法
    Python数字类型及字符串类型的内置方法 ##
    Python之流程控制
    前端混合
    数据库
    oracle 11g 安装过程
    SQLAlchemy
    pipreqs快速生成python项目所需的依赖
    llinux基本操作
    linux简介
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/7060009.html
Copyright © 2011-2022 走看看