Same Tree

mac2022-06-30  82

题目:

Given two binary trees, write a function to check if they are equal or not.

Two binary trees are considered equal if they are structurally identical and the nodes have the same value.

 

 

思路:和symmetric tree非常类似的递归解法

1 public boolean isSameTree(TreeNode p, TreeNode q) 2 { 3 if(p==null&&q==null) return true; 4 if(p==null||q==null) return false; 5 return (p.val == q.val) && isSameTree(p.left, q.left) && isSameTree(p.right, q.right); 6 7 }

 

转载于:https://www.cnblogs.com/hygeia/p/4695070.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)