20200326——剑指offer 面试题26:树的子结构

和链表相比,树中的指针操作更多也更复杂,因此与树相关的问题通常比链表要难。如果想加大面试的难度,则树的题目是很多面试官的选择。面对大量的指针操作,我们要更加小心,否则一不留神就会在代码中留下隐患。

现在回到这道题本身,要查找树A中是否存在和树B结构一样的子树。

第一步,在树A中找到和树B的根节点的值一样的节点R
第二步 判断A中以A为根节点的子树,是不是包含和树B一样的结构

package question26_find_tree_son;

/**
 * @Classname Solution
 * @Description TODO
 * @Date 2020/3/26 22:23
 * @Created by mmz
 */
public class Solution {
    class TreeNode{
        int val;
        TreeNode left = null   ;
        TreeNode right =  null  ;
    }

    public boolean DoesTreeHasTree(TreeNode root1,TreeNode root2){
        if(root2 == null){
            return true;
        }
        if(root1 == null){
            return false;
        }
        if(root1.val != root2.val){
            return false;
        }
        return DoesTreeHasTree(root1.right,root2.right) && DoesTreeHasTree(root1.left,root2.left);
    }

    public boolean HasSubTree(TreeNode root1,TreeNode root2){
        boolean result = false;
        if(root1 != null && root2 != null){
            if(root1.val == root2.val){
                result = DoesTreeHasTree(root1,root2);
            }
            if(!result){
                result = HasSubTree(root1.right,root2);
            }
            if(!result){
                result = HasSubTree(root1.left,root2);
            }
        }
        return result;
    }
}


二刷
package question26_树的子结构;

/**
 * @Classname Main
 * @Description TODO
 * @Date 2020/4/11 21:58
 * @Created by mmz
 */
public class Main {
    static class  TreeNode{
        int val;
        TreeNode left = null;
        TreeNode right = null;

        public TreeNode(int val) {
            this.val = val;
        }

        @Override
        public String toString() {
            return "TreeNode{" +
                    "val=" + val +
                    ", left=" + left +
                    ", right=" + right +
                    '}';
        }
    }

    static boolean Core(TreeNode root1,TreeNode root2){
        boolean result = false;
        if(root1 != null && root2 != null){
            if(root1.val == root2.val){
                result = Cores(root1,root2);
            }
            if(!result){
                result = Core(root1.right,root2);
            }
            if(!result){
                result = Core(root1.left,root2);
            }
        }
        return result;

    }

    static boolean Cores(TreeNode root1,TreeNode root2){
        boolean flag = false;
        if(root2 == null){
            return true;
        }
        if(root1 == null && root2 != null){
            return false;
        }
        if(root1.val == root2.val){
            return
            Cores(root1.right,root2.right) &&
            Cores(root1.left,root2.left);
        }else{
            return false;
        }
    }

    public static void main(String[] args) {
        TreeNode one = new TreeNode(8);
        TreeNode two = new TreeNode(8);
        TreeNode three = new TreeNode(7);
        TreeNode four = new TreeNode(9);
        TreeNode five = new TreeNode(2);
        TreeNode six = new TreeNode(4);
        TreeNode seven = new TreeNode(7);

        one.right = three;
        one.left = two;
        two.left = four;
        two.right = five;
        five.left = six;
        five.right = seven;

        TreeNode a = new TreeNode(8);
        TreeNode b = new TreeNode(6);
        TreeNode c = new TreeNode(2);
        a.left = b;
        a.right = c;

        System.out.println(Core(one, a));
    }
}

要考虑到遍历的问题,最后用的递归去做的。
在判断两个根节点的函数好做,怎么遍历要认真的复习一下。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值