面试题18 :树的子结构

题目:输入两颗二叉树 A 和 B,判断 B 是不是 A 的子结构。

先判断根结点,相等,判断左右孩子结点,有一个不是,重新寻找根结点。

package offer;
import java.sql.Struct;
//面试题 18:树的子结构
//题目:输入两颗二叉树 A 和 B,判断 B 是不是 A 的子结构。
public class Problem18 {
    static class BinaryTreeNode {
        int value;
        BinaryTreeNode leftNode;
        BinaryTreeNode rightNode;
    }
    public static void main(String args[]) {    
        BinaryTreeNode root1 = new BinaryTreeNode();
        BinaryTreeNode node1 = new BinaryTreeNode();
        BinaryTreeNode node2 = new BinaryTreeNode();
        BinaryTreeNode node3 = new BinaryTreeNode();
        BinaryTreeNode node4 = new BinaryTreeNode();
        BinaryTreeNode node5 = new BinaryTreeNode();
        BinaryTreeNode node6 = new BinaryTreeNode();
        root1.leftNode = node1;
        root1.rightNode = node2;
        node1.leftNode = node3;
        node1.rightNode = node4;
        node4.leftNode = node5;
        node4.rightNode = node6;
        root1.value = 8;
        node1.value = 8;
        node2.value = 7;
        node3.value = 9;
        node4.value = 2;
        node5.value = 4;
        node6.value = 7;
        BinaryTreeNode root2 = new BinaryTreeNode();
        BinaryTreeNode a = new BinaryTreeNode();
        BinaryTreeNode b = new BinaryTreeNode();
        root2.leftNode = a;
        root2.rightNode = b;
        root2.value = 8;
        a.value = 9;
        b.value = 2;
        Problem18 test = new Problem18();
        System.out.println(test.hasSubTree(root1, root2));
    }
    public boolean hasSubTree(BinaryTreeNode root1, BinaryTreeNode root2) {
        boolean result = false;
        if (root1 != null && root2 != null) {
            if (root1.value == root2.value) {
                result = doesTree1HavaTree2(root1, root2);
                if (!result)
                    result = hasSubTree(root1.leftNode, root2);
                if (!result)
                    result = hasSubTree(root1.rightNode, root2);
            }
        }
        return result;
    }
    private boolean doesTree1HavaTree2(BinaryTreeNode root1, BinaryTreeNode root2) {
        if (root2 == null) {
            return true;
        }else if (root1 == null)
            return false;
        if (root1.value != root2.value) {
            return false;
        }
        return doesTree1HavaTree2(root1.leftNode, root2.leftNode)
                && doesTree1HavaTree2(root1.rightNode, root2.rightNode);
    }
}

 

posted @ 2016-08-22 22:05  sunshinelym  阅读(168)  评论(0编辑  收藏  举报