当前位置:网站首页>力扣解法匯總513-找樹左下角的值
力扣解法匯總513-找樹左下角的值
2022-06-23 14:57:00 【失落夏天】
目錄鏈接:
力扣編程題-解法匯總_分享+記錄-CSDN博客
GitHub同步刷題項目:
https://github.com/September26/java-algorithms
原題鏈接:力扣
描述:
給定一個二叉樹的 根節點 root,請找出該二叉樹的 最底層 最左邊 節點的值。
假設二叉樹中至少有一個節點。
示例 1:
輸入: root = [2,1,3]
輸出: 1
示例 2:
輸入: [1,2,3,4,null,5,6,null,null,7]
輸出: 7
提示:
二叉樹的節點個數的範圍是 [1,104]
-231 <= Node.val <= 231 - 1
來源:力扣(LeetCode)
鏈接:https://leetcode.cn/problems/find-bottom-left-tree-value
著作權歸領扣網絡所有。商業轉載請聯系官方授權,非商業轉載請注明出處。
解題思路:
* 解題思路: * 用層序便利的方式可以解决,取最底層的list的第一個節點即可
代碼:
public class Solution513 {
public int findBottomLeftValue(TreeNode root) {
List<TreeNode> list = new ArrayList<>();
list.add(root);
TreeNode treeNode = levelSearch(list);
return treeNode.val;
}
private TreeNode levelSearch(List<TreeNode> list) {
List<TreeNode> nextList = new ArrayList<>();
for (TreeNode node : list) {
if (node.left != null) {
nextList.add(node.left);
}
if (node.right != null) {
nextList.add(node.right);
}
}
if (nextList.size() == 0) {
return list.get(0);
}
return levelSearch(nextList);
}
}边栏推荐
- Hot Recruitment! The second Tencent light · public welfare innovation challenge is waiting for you to participate
- 2021-05-22
- 2021-04-15
- Uniswap acquires genie, an NFT transaction aggregator. Will the NFT transaction market change?
- The well-known face search engine provokes public anger: just one photo will strip you of your pants in a few seconds
- Use of pyqt5 tool box
- 小米为何深陷芯片泥潭?
- Thinking and Practice on Quality Standardization (suitable for product, development, testing and management post learning)
- 图解OneFlow的学习率调整策略
- The team of China University of Mines developed an integrated multi-scale deep learning model for RNA methylation site prediction
猜你喜欢

2021-06-07

Auto - vérification recommandée! Les bogues MySQL ne font pas reculer les transactions, peut - être êtes - vous à risque!

图解OneFlow的学习率调整策略

大厂架构师:如何画一张大气的业务大图?

From the establishment to the actual combat of the robotframework framework, I won't just read this learning note

General sequence representation learning in kdd'22 "Ali" recommendation system
![[Level 2 warranty] which brand of Fortress machine is good for Level 2 warranty?](/img/84/4fa07c54786aaf423ca333ecde62f4.png)
[Level 2 warranty] which brand of Fortress machine is good for Level 2 warranty?

In this year's English college entrance examination, CMU delivered 134 high scores with reconstruction pre training, significantly surpassing gpt3

2021-04-15

The company has only one test, but the leader asked me to operate 1000 mobile numbers at the same time
随机推荐
物流贸易相关
WPF (c) new open source control library: newbeecoder UI waiting animation
一款自动生成单元测试的 IDEA 插件
ASP. Net C pharmacy management information system (including thesis) graduation project [demonstration video]
百万奖金等你来拿,首届中国元宇宙创新应用大赛联合创业黑马火热招募中!
SAP inventory gain / loss movement type 701 & 702 vs 711 & 712
Soaring 2000+? The average salary of software testing in 2021 has come out, and I can't sit still
2021-05-22
2022 ICT market in China continues to rise and enterprise digital infrastructure is imperative
Binding events of wechat applet in wx:for
【深入理解TcaplusDB技术】TcaplusDB构造数据
What is the charm of Guizhou? Why do Alibaba, Huawei and Tencent build data centers in Guizhou?
Thinking and Practice on Quality Standardization (suitable for product, development, testing and management post learning)
小米为何深陷芯片泥潭?
MySQL installation
In this year's English college entrance examination, CMU delivered 134 high scores with reconstruction pre training, significantly surpassing gpt3
Technology sharing | do you understand the requirements of the tested project?
K8s-- deploy stand-alone MySQL and persist it
Qu'est - ce que ça veut dire? Où dois - je m'inscrire?
2021-04-15