Skip to content

Commit c647379

Browse files
jojoxiaojingazl397985856
authored andcommitted
feat: azl397985856#236 add python 🐍 solution (azl397985856#278)
1 parent 34f3f06 commit c647379

File tree

1 file changed

+29
-52
lines changed

1 file changed

+29
-52
lines changed

problems/236.lowest-common-ancestor-of-a-binary-tree.md

Lines changed: 29 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -70,60 +70,11 @@ p and q are different and both values will exist in the binary tree.
7070

7171
## 代码
7272

73-
```js
73+
代码支持: JavaScript, Python3
7474

75+
- JavaScript Code:
7576

76-
/*
77-
* @lc app=leetcode id=236 lang=javascript
78-
*
79-
* [236] Lowest Common Ancestor of a Binary Tree
80-
*
81-
* https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/
82-
*
83-
* algorithms
84-
* Medium (35.63%)
85-
* Total Accepted: 267.3K
86-
* Total Submissions: 729.2K
87-
* Testcase Example: '[3,5,1,6,2,0,8,null,null,7,4]\n5\n1'
88-
*
89-
* Given a binary tree, find the lowest common ancestor (LCA) of two given
90-
* nodes in the tree.
91-
*
92-
* According to the definition of LCA on Wikipedia: “The lowest common ancestor
93-
* is defined between two nodes p and q as the lowest node in T that has both p
94-
* and q as descendants (where we allow a node to be a descendant of itself).”
95-
*
96-
* Given the following binary tree:  root = [3,5,1,6,2,0,8,null,null,7,4]
97-
*
98-
*
99-
*
100-
* Example 1:
101-
*
102-
*
103-
* Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
104-
* Output: 3
105-
* Explanation: The LCA of nodes 5 and 1 is 3.
106-
*
107-
*
108-
* Example 2:
109-
*
110-
*
111-
* Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
112-
* Output: 5
113-
* Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a descendant
114-
* of itself according to the LCA definition.
115-
*
116-
*
117-
*
118-
*
119-
* Note:
120-
*
121-
*
122-
* All of the nodes' values will be unique.
123-
* p and q are different and both values will exist in the binary tree.
124-
*
125-
*
126-
*/
77+
```js
12778
/**
12879
* Definition for a binary tree node.
12980
* function TreeNode(val) {
@@ -147,6 +98,32 @@ var lowestCommonAncestor = function(root, p, q) {
14798
};
14899
```
149100

101+
- Python Code:
102+
103+
``` python
104+
# Definition for a binary tree node.
105+
# class TreeNode:
106+
# def __init__(self, x):
107+
# self.val = x
108+
# self.left = None
109+
# self.right = None
110+
111+
class Solution:
112+
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
113+
if not root or root == p or root == q:
114+
return root
115+
left = self.lowestCommonAncestor(root.left, p, q)
116+
right = self.lowestCommonAncestor(root.right, p, q)
117+
118+
if not left:
119+
return right
120+
if not right:
121+
return left
122+
else:
123+
return root
124+
125+
```
126+
150127
## 扩展
151128
如果递归的结束条件改为`if (!root || root.left === p || root.right === q) return root;` 代表的是什么意思,对结果有什么样的影响?
152129

0 commit comments

Comments
 (0)