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; } }
原文地址:https://www.cnblogs.com/stono/p/9540289.html
时间: 2024-10-16 00:15:19