Browse Source

leetcode 993

master
OhYee 1 year ago
parent
commit
5f1c64422e
Signed by: OhYee
GPG Key ID: 5A9E1F63ED274FBB
  1. 33
      leetcode/993/993.go

33
leetcode/993/993.go

@ -0,0 +1,33 @@ @@ -0,0 +1,33 @@
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isCousins(root *TreeNode, x int, y int) bool {
var px, py *TreeNode
var dx, dy int
var dfs func (*TreeNode, *TreeNode, int)
dfs = func (parent *TreeNode, cur *TreeNode, depth int) {
if cur == nil {
return
}
if cur.Val == x {
px = parent
dx = depth
}
if cur.Val == y {
py = parent
dy = depth
}
dfs(cur, cur.Left, depth + 1)
dfs(cur, cur.Right, depth + 1)
}
dfs(nil, root, 0)
return px != py && dx == dy
}
Loading…
Cancel
Save