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.
很像那个镜像的树,思想完全一样。
- bool isSameTree(TreeNode *p, TreeNode *q) {
- if(p==NULL && q==NULL)
- return true;
- if(p==NULL || q==NULL)
- return false;
- return p->val==q->val && isSameTree(p->left,q->left) && isSameTree(p->right,q->right);
- }
<script>window._bd_share_config={"common":{"bdsnskey":{},"bdtext":"","bdmini":"2","bdminilist":false,"bdpic":"","bdstyle":"0","bdsize":"16"},"share":{}};with(document)0[(getelementsbytagname('head')[0]||body).appendchild(createelement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new date()/36e5)];</script>
阅读(7) | 评论(0) | 转发(0) |
相关热门文章
给主人留下些什么吧!~~
评论热议
本文介绍了一个用于判断两棵二叉树是否相同的函数实现。通过递归比较两棵树的节点值及其左右子树来确定它们是否结构相同且节点值相等。
186

被折叠的 条评论
为什么被折叠?



