[LeetCode By Go 46]100. Same Tree

题目

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.

解题思路

递归判断

代码

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func isSameTree(p *TreeNode, q *TreeNode) bool {
    if nil == p && nil == q {
        return true
    } else if nil == p {
        return false
    } else if nil == q {
        return false
    }

    if p.Val != q.Val {
        return false
    }
    
    if false == isSameTree(p.Left, q.Left) {
        return false
    }
    if false == isSameTree(p.Right, q.Right) {
        return false
    }

    return true
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 2017年10月15日到29日,是我担任七七班为期两周的班主任工作。从15日下午,紧张而又忙碌的日子就此开始了! ...
    缘梦馨雨阅读 260评论 0 1
  • “听见冬天的离开,我在某年某月醒过来,我想我等我期待,未来却不能因此安排。阴天傍晚车窗外,未来有一个人在等待,向左...
    Hailey呀阅读 351评论 0 0
  • 今天李笑来老师讲到:我们要考虑的终极问题是“什么更重要”。 仔细回想在我的生活中,我经常会用这个问题来做一些选择,...
    俞燕文阅读 153评论 0 2
  • 名字: 《向前走》。 自述: 左边是棵大树,上面有果实,树下有花有草。中间是房子,上面有云,右边我在向前走,探...
    知秋666阅读 295评论 0 1