当前位置: 代码网 > it编程>软件设计>算法 > LeetCode - Medium - 1123

LeetCode - Medium - 1123

2024年07月31日 算法 我要评论
(img-rALjdppc-1718902274431)](img-BhtRqIiX-1718902274432)](img-3iF0novm-1718902274432)](img-flDLnE8h-1718902274433)](img-RKtGg8CV-1718902274434)](img-BSEpiEMS-1718902274434)](img-WZIP8a5a-1718902274435)]//方法一:自己写的,用后序遍历模式。//方法二:别人写的,用后序遍历模式。

description


https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves/

given the root of a binary tree, return the lowest common ancestor of its deepest leaves.

recall that:

  • the node of a binary tree is a leaf if and only if it has no children

  • the depth of the root of the tree is 0. if the depth of a node is d, the depth of each of its children is d + 1.

  • the lowest common ancestor of a set s of nodes, is the node a with the largest depth such that every node in s is in the subtree with root a.

note: this question is the same as 865: https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/

example 1:

在这里插入图片描述

input: root = [3,5,1,6,2,0,8,null,null,7,4]

output: [2,7,4]

explanation: we return the node with value 2, colored in yellow in the diagram.

the nodes coloured in blue are the deepest leaf-nodes of the tree.

note that nodes 6, 0, and 8 are also leaf nodes, but the depth of them is 2, but the depth of nodes 7 and 4 is 3.

example 2:

input: root = [1]

output: [1]

explanation: the root is the deepest node in the tree, and it’s the lca of itself.

example 3:

input: root = [0,1,3,null,2]

output: [2]

explanation: the deepest leaf node in the tree is 2, the lca of one node is itself.

constraints:

  • the number of nodes in the tree will be in the range [ 1 , 1000 ] [1, 1000] [1,1000].

  • 0 < = n o d e . v a l < = 1000 0 <= node.val <= 1000 0<=node.val<=1000

  • the values of the nodes in the tree are unique.

analysis


leetcode - medium - 236. lowest common ancestor of a binary tree启发。

方法一:自己写的,用后序遍历模式

方法二:别人写的,用后序遍历模式

submission


import com.lun.util.binarytree.treenode;

public class lowestcommonancestorofdeepestleaves {

//方法一:自己写的,用后序遍历模式

public treenode lcadeepestleaves(treenode root) {

object[] result = lcadeepestleaves(root, 0);

return result != null ? (treenode)result[0] : null;

}

private object[] lcadeepestleaves(treenode node, int depth) {

if(node == null) return null;

depth++;

object[] leftresult = lcadeepestleaves(node.left, depth);

object[] rightresult = lcadeepestleaves(node.right, depth);

if(leftresult == null && rightresult == null) {//叶子节点

return new object[] {node, depth};

}else if(leftresult != null && rightresult == null){

return leftresult;

}else if(leftresult == null && rightresult != null) {

return rightresult;

}else {

int leftdepth = (int)leftresult[1];

int rightdepth = (int)rightresult[1];

if(leftdepth > rightdepth) {

return leftresult;

}else if(leftdepth < rightdepth) {

return rightresult;

}else {

leftresult[0] = node;

return leftresult;

}

}

}

//方法二:别人写的,用后序遍历模式

public treenode lcadeepestleaves2(treenode root) {

pair p = getlca(root, 0);

return p.node;

}

private pair getlca(treenode root, int d) {

if (root == null) return new pair(null, d);

pair l = getlca(root.left, d + 1);

pair r = getlca(root.right, d + 1);

if (l.d == r.d) {

return new pair(root, l.d);

} else {

return l.d > r.d ? l : r;

}

}

private class pair {

treenode node;

int d;

pair(treenode node, int d) {

this.node = node;

this.d = d;

}

}

最后

分享一些系统的面试题,大家可以拿去刷一刷,准备面试涨薪。

这些面试题相对应的技术点:

  • jvm
  • mysql
  • mybatis
  • mongodb
  • redis
  • spring
  • spring boot
  • spring cloud
  • kafka
  • rabbitmq
  • nginx

大类就是:

  • java基础
  • 数据结构与算法
  • 并发编程
  • 数据库
  • 设计模式
  • 微服务
  • 消息中间件

程序员,每个月给你发多少工资,你才会想老板想的事?

程序员,每个月给你发多少工资,你才会想老板想的事?

程序员,每个月给你发多少工资,你才会想老板想的事?

程序员,每个月给你发多少工资,你才会想老板想的事?

程序员,每个月给你发多少工资,你才会想老板想的事?

程序员,每个月给你发多少工资,你才会想老板想的事?

程序员,每个月给你发多少工资,你才会想老板想的事?

程序员,每个月给你发多少工资,你才会想老板想的事?

程序员,每个月给你发多少工资,你才会想老板想的事?

[外链图片转存中…(img-raljdppc-1718902274431)]

[外链图片转存中…(img-bhtrqiix-1718902274432)]

[外链图片转存中…(img-3if0novm-1718902274432)]

[外链图片转存中…(img-fldlne8h-1718902274433)]

[外链图片转存中…(img-rktgg8cv-1718902274434)]

[外链图片转存中…(img-bsepiems-1718902274434)]

[外链图片转存中…(img-wzip8a5a-1718902274435)]

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com