判断两颗树是否相同

tag: 二叉树

 

package com.zhaochao.tree;

import java.util.Stack;

/**
 * Created by zhaochao on 17/1/24.
 * 两颗树相等,意味着 对应节点的值相等,且具备相同的左右子树
 */
public class JudgeSameTree {

    //recursion
public boolean isSame(TreeNode rootA, TreeNode rootB) {

    if(rootA == null && rootB == null) {
        return true;
    }

    if(rootA == null || rootB == null) {
        return false;
    }

    if(rootA.val != rootB.val) {
        return false;
    }

    return isSame(rootA.left, rootB.left) && isSame(rootA.right, rootB.right);
}

    public boolean isSameTraversal(TreeNode rootA, TreeNode rootB) {
        if(rootA == null && rootB == null) {
            return true;
        }
        if(rootA == null || rootB == null) {
            return false;
        }
        if(rootA.val != rootB.val) {
            return false;
        }

        Stack<TreeNode> stackA = new Stack<TreeNode>();
        Stack<TreeNode> stackB = new Stack<TreeNode>();

        stackA.push(rootA);
        stackB.push(rootB);

        while((!stackA.isEmpty() && !stackB.isEmpty())) {

            TreeNode nodeA = stackA.pop();
            TreeNode nodeB = stackB.pop();

            if(nodeA.val != nodeB.val) {
                return false;
            }

            if(nodeA.right != null && nodeB.right != null) {
                stackA.push(nodeA.right);
                stackB.push(nodeB.right);
            }
            else if(nodeA.right != null || nodeB.right != null) {
                return false;
            }

            if(nodeA.left != null && nodeB.left != null) {
                stackA.push(nodeA.left);
                stackB.push(nodeB.left);
            }
            else if(nodeA.left != null || nodeB.left != null) {
                return false;
            }

        }

        return true;
    }


    public static void main(String[] args) {

        TreeNode root = new TreeNode(0);
        TreeNode node1 = new TreeNode(1);
        TreeNode node2 = new TreeNode(2);
        TreeNode node3 = new TreeNode(3);
        //TreeNode node4 = new TreeNode(4);

        root.left = node1;
        root.right = node2;
        node2.left = node3;
        //node3.left = node4;

        TreeNode roota = new TreeNode(0);
        TreeNode nodea1 = new TreeNode(1);
        TreeNode nodea2 = new TreeNode(2);
        TreeNode nodea3 = new TreeNode(3);

        roota.left = nodea1;
        roota.right = nodea2;
        nodea2.left = nodea3;

        JudgeSameTree test  =  new JudgeSameTree();
        boolean result = test.isSameTraversal(root,roota);

        System.out.println(" is same : " + result);
    }

}

  

转载于:https://www.cnblogs.com/superzhaochao/p/6348022.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值