100. 相同的树
Posted stono
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了100. 相同的树相关的知识,希望对你有一定的参考价值。
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. 相同的树的主要内容,如果未能解决你的问题,请参考以下文章