zoukankan      html  css  js  c++  java
  • 剑指offer_17:树的子结构

    输入两棵二叉树A和B,判断B是不是A的子结构。(约定空树不是任意一个树的子结构)

    B是A的子结构, 即 A中有出现和B相同的结构和节点值。

    例如:
    给定的树 A:

    给定的树 B:

    返回 true,因为 B 与 A 的一个子树拥有相同的结构和节点值。

    示例 1:
    输入:A = [1,2,3], B = [3,1]
    输出:false

    示例 2:
    输入:A = [3,4,5,1,2], B = [4,1]
    输出:true

    限制:
    0 <= 节点个数 <= 10000

    1、递归

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public boolean isSubStructure(TreeNode A, TreeNode B) {
            //特殊情况
            if(A==null||B==null){
                return false;
            }
            //A、B结点相等就开始比较子树是否相等
            if(A.val==B.val&&helper(A.left,B.left)&&helper(A.right,B.right)){
                return true;
            }
            //扫描A直到找到和B相等的点
            return isSubStructure(A.left,B)||isSubStructure(A.right,B);
        }
        public boolean helper(TreeNode A,TreeNode B){
            //B为空则结束
            if(B==null) return true;
            //A为空则无法匹配
            if(A==null) return false;
            //比较A和B的子树
            if(A.val==B.val){
                return helper(A.left,B.left)&&helper(A.right,B.right);
            }
            return false;
        }
    }
    

    2、广度优先搜索

    因为要遍历二叉树,用深搜的递归、非递归,或者广搜都是可以的

    但是用广搜会超时

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public boolean isSubStructure(TreeNode A, TreeNode B) {
            if(A==null||B==null){
                return false;
            }
            Queue<TreeNode> list=new LinkedList<>();
            list.offer(A);
            while(!list.isEmpty()){
                TreeNode node=list.poll();
                if(bfs(node,B)){
                    return true;
                }
                if(node.left!=null){
                    list.offer(node.left);
                }
                if(node.right!=null){
                    list.offer(node.right);
                }
            }
            return false;
        }
        public boolean bfs(TreeNode node,TreeNode B){
            Queue<TreeNode> queueA=new LinkedList<>();
            queueA.offer(node);
            Queue<TreeNode> queueB=new LinkedList<>();
            queueB.offer(B);
            while(!queueA.isEmpty()&&!queueB.isEmpty()){
                TreeNode nodeA=queueA.poll();
                TreeNode nodeB=queueB.poll();
                if(nodeA==null&&nodeB!=null){
                    return false;//A走完,B未走完,必定无法比配
                }
                if(nodeB==null){
                    continue;//该B结点为空,但同一层可能还有非空的B结点
                }
                if(nodeA.val!=nodeB.val){
                    return false;
                }
                queueA.offer(node.left);
                queueA.offer(node.right);
                queueB.offer(B.left);
                queueB.offer(B.right);
            }
            return true;
        }
    }
    
  • 相关阅读:
    动态显示和隐藏状态栏(包括底部虚拟按键)
    android平台手电筒开发源代码
    实现IOS圆角风格的列表ListView
    在android中使用achartengine来绘制各种图表
    模拟iOS系统原生导航条隐藏或显示动画
    调整label中text显示的行间距
    Reinforcement Learning Using a Continuous Time Actor-Critic Framework with Spiking Neurons
    A neural reinforcement learning model for tasks with unknown time delays
    Event-driven Random Backpropagation: Enabling Neuromorphic Deep Learning Machines
    S4NN: temporal backpropagation for spiking neural networks with one spike per neuron
  • 原文地址:https://www.cnblogs.com/xyz-1024/p/14090445.html
Copyright © 2011-2022 走看看