C++
Java
Python
Python3
C#
JavaScript
monokai
ambiance
chaos
chrome
cloud9_day
cloud9_night
cloud9_night_low_color
clouds
clouds_midnight
cobalt
crimson_editor
dawn
dracula
dreamweaver
eclipse
github
github_dark
gob
gruvbox
gruvbox_dark_hard
gruvbox_light_hard
idle_fingers
iplastic
katzenmilch
kr_theme
kuroir
merbivore
merbivore_soft
mono_industrial
nord_dark
one_dark
pastel_on_dark
solarized_dark
solarized_light
sqlserver
terminal
textmate
tomorrow
tomorrow_night
tomorrow_night_blue
tomorrow_night_bright
tomorrow_night_eighties
twilight
vibrant_ink
xcode
上次编辑到这里,代码来自缓存 点击恢复默认模板
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, vector<TreeNode*> &nodes) {
}
};
运行代码
提交
java 解法, 执行用时: 0 ms, 内存消耗: 43.3 MB, 提交时间: 2023-10-16 21:54:21
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode[] nodes) {
if (root==null){
return null;
}
for (TreeNode node:nodes){
if (node==root){
return root;
}
}
TreeNode left=lowestCommonAncestor(root.left,nodes);
TreeNode right=lowestCommonAncestor(root.right,nodes);
if (left!=null && right!=null){
return root;
}
return left!=null?left:right;
}
}
java 解法, 执行用时: 0 ms, 内存消耗: 43.3 MB, 提交时间: 2023-10-16 21:54:01
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode[] nodes) {
if (root == null) {
return null;
}
if (isIn(nodes, root)) {
return root;
} else {
TreeNode res = null;
TreeNode left = lowestCommonAncestor(root.left, nodes);
TreeNode right = lowestCommonAncestor(root.right, nodes);
if (left != null && right != null) {
return root;
}
res = (left == null) ? right : left;
return res;
}
}
public boolean isIn(TreeNode[] nodes, TreeNode root) {
for (int i = 0; i < nodes.length; i++) {
if (nodes[i] == root) {
return true;
}
}
return false;
}
}
python3 解法, 执行用时: 124 ms, 内存消耗: 21.3 MB, 提交时间: 2023-10-16 21:53:37
# 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', nodes: 'List[TreeNode]') -> 'TreeNode':
# dfs_LRN 后序遍历求LCA
if root==None or root in nodes:
return root
L = self.lowestCommonAncestor(root.left, nodes)
R = self.lowestCommonAncestor(root.right, nodes)
if L and R:
return root
elif L and not R:
return L
elif not L and R:
return R
else:
return None
cpp 解法, 执行用时: 56 ms, 内存消耗: 41.4 MB, 提交时间: 2023-10-16 21:53:23
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, vector<TreeNode*>& nodes) {
if (!root) return root;
if (count(nodes.begin(), nodes.end(), root))
return root;
TreeNode* left = lowestCommonAncestor(root->left, nodes);
TreeNode* right = lowestCommonAncestor(root->right, nodes);
if (left && right)
return root;
return left ? left : right;
}
};
javascript 解法, 执行用时: 100 ms, 内存消耗: 57.1 MB, 提交时间: 2023-10-16 21:52:41
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @param {TreeNode[]} nodes
* @return {TreeNode}
*/
var lowestCommonAncestor = function(root, nodes) {
if(!root || nodes.includes(root)) return root
let left = lowestCommonAncestor(root.left,nodes)
let right = lowestCommonAncestor(root.right,nodes)
if(left && right) return root
return left || right
};