题目:Same Tree
/** * LeetCode Same Tree * 题目:判断两棵树是否是完全相同的 * 思路:完全相同:1、结构相同; * 2、值相同。 * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ package javaTrain; public class Train7 { public boolean isSameTree(TreeNode p, TreeNode q) { if(p == null && q == null) return true; else if(p == null || q == null) return false; if(p.val == q.val){ return isSameTree(p.left,q.left) && isSameTree(p.right,q.right); } else return false; } }
时间: 2024-10-09 23:29:13