码迷,mamicode.com
首页 > 其他好文 > 详细

100. 相同的树

时间:2018-08-27 10:29:38      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:problems   amp   oid   ==   print   col   alq   sametree   string   

100. 相同的树

https://leetcode-cn.com/problems/same-tree/description/

package com.test;

/**
 * @Author stono
 * @Date 2018/8/27 上午8:59
 */
public class Lesson100 {
    public static void main(String[] args) {
        TreeNode t1 = new TreeNode(1);
        TreeNode t2 = new TreeNode(2);
        TreeNode t3 = new TreeNode(3);
        TreeNode n1 = new TreeNode(1);
        TreeNode n2 = new TreeNode(2);
        TreeNode n3 = new TreeNode(3);
        t1.left = t2;
        t1.right = t3;
        n1.left = n2;
        n1.right = n3;
        boolean sameTree = isSameTree(t1, n1);
        System.out.println(sameTree);
    }

    public static boolean isSameTree(TreeNode p, TreeNode q) {
        if (p == null && q == null) {
            return true;
        }
        if (p == null && q != null) {
            return false;
        }
        if (p != null && q == null) {
            return false;
        }
        int valp = p.val;
        int valq = q.val;
        if (valp - valq == 0) {
            // 递归调用,进行判断
            return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
        }
        return false;
    }
}

 

100. 相同的树

标签:problems   amp   oid   ==   print   col   alq   sametree   string   

原文地址:https://www.cnblogs.com/stono/p/9540289.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!