1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| /** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode { if p.Val > q.Val { return lowestCommonAncestor(root, q, p) } if root.Val < p.Val { return lowestCommonAncestor(root.Right, q, p) } else if root.Val > q.Val { return lowestCommonAncestor(root.Left, q, p) } return root }
|