zoukankan      html  css  js  c++  java
  • 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.

    因为CSDN的java模版有问题,无法显示null,这里改用c++模版显示

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public boolean isSubtree(TreeNode s, TreeNode t) {
            if (s == null || t == null) return s == t;
            if (isSame(s, t))   return true;
            return isSubtree(s.left, t) || isSubtree(s.right, t);
        }
        public boolean isSame(TreeNode s, TreeNode t) {
            if (s == null || t == null) return s == t;
            return s.val == t.val && isSame(s.left, t.left) && isSame(s.right, t.right);
        }
    }


    题目输入的测试是s是[3, 4, 5, 1, 2, null, null, 0],t是[4, 1, 2]的形式,从上到下按层遍历建树

    Debug code in playground:

    /* -----------------------------------
     *  WARNING:
     * -----------------------------------
     *  Your code may fail to compile
     *  because it contains public class
     *  declarations.
     *  To fix this, please remove the
     *  "public" keyword from your class
     *  declarations.
     */
    
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public boolean isSubtree(TreeNode s, TreeNode t) {
            if (s == null || t == null) return s == t;
            if (isSame(s, t))   return true;
            return isSubtree(s.left, t) || isSubtree(s.right, t);
        }
        public boolean isSame(TreeNode s, TreeNode t) {
            if (s == null || t == null) return s == t;
            return s.val == t.val && isSame(s.left, t.left) && isSame(s.right, t.right);
        }
    }
    
    public class MainClass {
        public static TreeNode stringToTreeNode(String input) {
            input = input.trim();
            input = input.substring(1, input.length() - 1);
            if (input.length() == 0) {
                return null;
            }
        
            String[] parts = input.split(",");
            String item = parts[0];
            TreeNode root = new TreeNode(Integer.parseInt(item));
            Queue<TreeNode> nodeQueue = new LinkedList<>();
            nodeQueue.add(root);
        
            int index = 1;
            while(!nodeQueue.isEmpty()) {
                TreeNode node = nodeQueue.remove();
        
                if (index == parts.length) {
                    break;
                }
        
                item = parts[index++];
                item = item.trim();
                if (!item.equals("null")) {
                    int leftNumber = Integer.parseInt(item);
                    node.left = new TreeNode(leftNumber);
                    nodeQueue.add(node.left);
                }
        
                if (index == parts.length) {
                    break;
                }
        
                item = parts[index++];
                item = item.trim();
                if (!item.equals("null")) {
                    int rightNumber = Integer.parseInt(item);
                    node.right = new TreeNode(rightNumber);
                    nodeQueue.add(node.right);
                }
            }
            return root;
        }
        
        public static String booleanToString(boolean input) {
            return input ? "True" : "False";
        }
        
        public static void main(String[] args) throws IOException {
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            String line;
            while ((line = in.readLine()) != null) {
                TreeNode s = stringToTreeNode(line);
                line = in.readLine();
                TreeNode t = stringToTreeNode(line);
                
                boolean ret = new Solution().isSubtree(s, t);
                
                String out = booleanToString(ret);
                
                System.out.print(out);
            }
        }
    }


    ========================================Talk is cheap, show me the code=======================================

    CSDN博客地址:https://blog.csdn.net/qq_34115899
  • 相关阅读:
    导航栏的修改
    [题解](背包)luogu_P4095 eden的新背包问题
    [題解](貪心/堆)luogu_P2107小Z的AK計劃
    [題解](最短路)luogu_P2384最短路
    [題解](單調隊列dp)luogu_P1725琪露諾
    [題解](單調隊列/水)luogu_P3088擠奶牛
    [題解](單調隊列dp)【2016noip福建夏令營】探險
    [題解](水/最短路)出题人大战工作人员
    [题解](最短路)最短路点数
    [題解]luogu_P1613跑路(最短路/倍增)
  • 原文地址:https://www.cnblogs.com/lcy0515/p/9179754.html
Copyright © 2011-2022 走看看