zoukankan      html  css  js  c++  java
  • leetcode 572. 另一个树的子树 【时间击败88.10%】 【内存击败96.40%】

     1 boolean ans = false;
     2 
     3     public boolean isSubtree(TreeNode s, TreeNode t) {
     4         if (t == null) return true;
     5         if (s == null) return false;
     6         dfs(s, t);
     7         return ans;
     8     }
     9 
    10     void dfs(TreeNode s, TreeNode t) {
    11         if (s.val == t.val) {
    12             if (check(s, t)) {
    13                 ans = true;
    14                 return;
    15             }
    16         }
    17         if (s.left != null) dfs(s.left, t);
    18         if (s.right != null) dfs(s.right, t);
    19     }
    20 
    21     boolean check(TreeNode s, TreeNode t) {
    22         if (s == null && t == null) return true;
    23         if (s == null || t == null) return false;
    24         if (s.val == t.val) {
    25             return check(s.left, t.left) && check(s.right, t.right);
    26         }
    27         return false;
    28     }
     
     
     
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    几道cf水题
    cf水题
    一道cf水题
    c++list用法
    c++map用法
    c++ vector常见用法
    c++string,常见用法总结
    复变函数考试后的反思
    [FZYZOJ 1204] 零和问题
    [FZYZOJ 1202] 金坷垃
  • 原文地址:https://www.cnblogs.com/towerbird/p/11576766.html
Copyright © 2011-2022 走看看