Leetcode - Power of Four
Leetcode - Letter Combinations of a Phone Number

Leetcode - Symmetric Tree

violet posted @ Jun 09, 2020 03:11:18 AM in 算法 with tags Algorithm Golang tree , 297 阅读

https://leetcode.com/problems/symmetric-tree/

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree [1,2,2,3,4,4,3] is symmetric:

    1
   / \
  2   2
 / \ / \
3  4 4  3

 

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func isSymmetric(root *TreeNode) bool {
    if root == nil {
        return true
    }
    return check(root.Left, root.Right)
}

func check(p, q *TreeNode) bool {
    if p == nil && q == nil {
        return true
    }
    if p == nil || q == nil {
        return false
    }
    return p.Val == q.Val && check(p.Left, q.Right) && check(p.Right, q.Left)
}
Russian Escorts in D 说:
Oct 10, 2021 07:40:52 PM

It is unique to see things like this. It is a great scene.

Bhawna 说:
Feb 14, 2022 03:27:43 PM

violet's Blog is a wonderful and amazing blog for readers. Sexy Delhi Escort Hot Delhi Escorts[/url]


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter