[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
}
    原文作者:miltonsun
    原文地址: https://www.jianshu.com/p/285378f9a8b0
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞