2024-02-09发表2024-02-09更新LeetCode每日一题1 分钟读完 (大约112个字)236. 二叉树的最近公共祖先236. 二叉树的最近公共祖先 难度: medium 原始链接: https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-tree 标签: 后序遍历 解法一: 后序遍历go123456789101112131415161718192021/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode { if root == nil || root == p || root == q { return root } leftNode := lowestCommonAncestor(root.Left, p, q) rightNode := lowestCommonAncestor(root.Right, p, q) if leftNode == nil { return rightNode } else if rightNode == nil { return leftNode } return root} 236. 二叉树的最近公共祖先https://wuhunyu.top/leetcode/2024/02/lowest-common-ancestor-of-a-binary-tree/index.html作者wuhunyu发布于2024-02-09更新于2024-02-09许可协议#后序遍历