File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed
docs/data-structure/tree/recursion Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -729,6 +729,41 @@ class Solution:
729
729
return root
730
730
```
731
731
732
+ #### ** Go**
733
+
734
+ ``` go
735
+ /* *
736
+ * Definition for TreeNode.
737
+ * type TreeNode struct {
738
+ * Val int
739
+ * Left *ListNode
740
+ * Right *ListNode
741
+ * }
742
+ */
743
+ func lowestCommonAncestor (root , p , q *TreeNode ) *TreeNode {
744
+ if root == nil || root == p || root == q {
745
+ return root
746
+ }
747
+
748
+ // 寻找左子树
749
+ left := lowestCommonAncestor (root.Left , p, q)
750
+ // 寻找右子树
751
+ right := lowestCommonAncestor (root.Right , p, q)
752
+
753
+ if left == nil && right == nil {
754
+ return nil
755
+ }
756
+ if left == nil {
757
+ return right
758
+ }
759
+ if right == nil {
760
+ return left
761
+ }
762
+
763
+ return root
764
+ }
765
+ ```
766
+
732
767
<!-- tabs:end -->
733
768
734
769
## 337. 打家劫舍 III
You can’t perform that action at this time.
0 commit comments