From c64737924fd441aaa1f1c8a5102dbf6302bb9417 Mon Sep 17 00:00:00 2001 From: Jojo Date: Thu, 16 Jan 2020 01:00:30 -0800 Subject: [PATCH] =?UTF-8?q?feat:=20#236=20add=20python=20=F0=9F=90=8D=20so?= =?UTF-8?q?lution=20=20(#278)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...lowest-common-ancestor-of-a-binary-tree.md | 81 +++++++------------ 1 file changed, 29 insertions(+), 52 deletions(-) diff --git a/problems/236.lowest-common-ancestor-of-a-binary-tree.md b/problems/236.lowest-common-ancestor-of-a-binary-tree.md index 779d62108..f5a85d6e0 100644 --- a/problems/236.lowest-common-ancestor-of-a-binary-tree.md +++ b/problems/236.lowest-common-ancestor-of-a-binary-tree.md @@ -70,60 +70,11 @@ p and q are different and both values will exist in the binary tree. ## 代码 -```js +代码支持: JavaScript, Python3 +- JavaScript Code: -/* - * @lc app=leetcode id=236 lang=javascript - * - * [236] Lowest Common Ancestor of a Binary Tree - * - * https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/ - * - * algorithms - * Medium (35.63%) - * Total Accepted: 267.3K - * Total Submissions: 729.2K - * Testcase Example: '[3,5,1,6,2,0,8,null,null,7,4]\n5\n1' - * - * Given a binary tree, find the lowest common ancestor (LCA) of two given - * nodes in the tree. - * - * According to the definition of LCA on Wikipedia: “The lowest common ancestor - * is defined between two nodes p and q as the lowest node in T that has both p - * and q as descendants (where we allow a node to be a descendant of itself).” - * - * Given the following binary tree:  root = [3,5,1,6,2,0,8,null,null,7,4] - * - * - * - * Example 1: - * - * - * Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1 - * Output: 3 - * Explanation: The LCA of nodes 5 and 1 is 3. - * - * - * Example 2: - * - * - * Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4 - * Output: 5 - * Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a descendant - * of itself according to the LCA definition. - * - * - * - * - * Note: - * - * - * All of the nodes' values will be unique. - * p and q are different and both values will exist in the binary tree. - * - * - */ +```js /** * Definition for a binary tree node. * function TreeNode(val) { @@ -147,6 +98,32 @@ var lowestCommonAncestor = function(root, p, q) { }; ``` +- Python Code: + +``` python +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution: + def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode': + if not root or root == p or root == q: + return root + left = self.lowestCommonAncestor(root.left, p, q) + right = self.lowestCommonAncestor(root.right, p, q) + + if not left: + return right + if not right: + return left + else: + return root + +``` + ## 扩展 如果递归的结束条件改为`if (!root || root.left === p || root.right === q) return root;` 代表的是什么意思,对结果有什么样的影响?