美文网首页LeetCode By Go
[LeetCode By Go 46]100. Same Tre

[LeetCode By Go 46]100. Same Tre

作者: miltonsun | 来源:发表于2017-08-22 16:41 被阅读3次

题目

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
}

相关文章

网友评论

    本文标题:[LeetCode By Go 46]100. Same Tre

    本文链接:https://www.haomeiwen.com/subject/jqmldxtx.html