leetcode_513.Find Bottom Left Tree Value找树最后一行的最左数
来源:互联网 发布:逆波兰式算法c 编辑:程序博客网 时间:2024/06/14 15:58
Given a binary tree, find the leftmost value in the last row of the tree.
Example 1:
Input:
2
/ \
1 3
Output:
1
Example 2:
Input:
1 / \ 2 3 / / \4 5 6 / 7
Output:
7
析:开始自己写的是判断数的深度,如果左子树深度大于右子树,就走左否则走右。但是时间超时。
解法:每次把一行的数据保存下来,找第一个即可
用List存储一行。
public int findBottomLeftValue(TreeNode root) { int res = root.val; // list1存放树中当前行的数据 List<TreeNode> list1 = new ArrayList<>(); List<TreeNode> list2 = new ArrayList<>(); list1.add(root); while(!list1.isEmpty()){ res = list1.get(0).val; list2.clear(); for(int i=0;i<list1.size();i++){ if(list1.get(i).left!=null) list2.add(list1.get(i).left); if(list1.get(i).right!=null) list2.add(list1.get(i).right); } list1.clear(); list1.addAll(list2); } return res; }
0 0
- leetcode_513.Find Bottom Left Tree Value找树最后一行的最左数
- 【Leetcode513 Find Bottom left Tree Value】树的遍历
- Find Bottom Left Tree Value
- Find Bottom Left Tree Value
- Find Bottom Left Tree Value
- Find Bottom Left Tree Value
- Find Bottom Left Tree Value
- Find Bottom Left Tree Value
- Find Bottom Left Tree Value
- Find Bottom Left Tree Value
- Find Bottom Left Tree Value
- Find Bottom Left Tree Value
- Find Bottom Left Tree Value
- Find Bottom Left Tree Value |找到二叉树最后一层的最左边的节点的值
- 513. Find Bottom Left Tree Value 树 BFS
- LeetCode 513. Find Bottom Left Tree Value
- 513:find bottom left tree value
- [leetcode]513. Find Bottom Left Tree Value
- L1-021. 重要的话说三遍
- 【CSS】请不要问我们垂直水平居中
- PC端各浏览器布局兼容问题【Chrome、Firefox、IE】——长期更新
- 线程的生命周期和状态
- [题解] USACO cow hopscotch(牛跳房子)
- leetcode_513.Find Bottom Left Tree Value找树最后一行的最左数
- 复杂网络常见的数据和程序代码网站
- 哪里可以找到前端开发的最新资讯?
- Android 极光推送使用
- iproute2安装
- Proteus安装
- 【算法】排序算法(四)——交换排序
- 虚拟机和物理机之间的传输的六种方法
- C# 高级编程