LeetCode赛题----Find Left Most Element
问题描述
Given a binary tree, find the left most element 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
Note: You may assume the tree is not NULL.
算法分析:
逐层遍历二叉树是很经典的算法,常规的逐层遍历二叉树是使用一个队列,每次从队列中取出一个节点,将节点的左右子节点加入队列尾部。但这种算法无法对二叉树的各个层进行区分。所以这里需要有两个队列,以此区分各个层。
Java算法实现:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int findLeftMostNode(TreeNode root) {
TreeNode leftMost=root;
Queue<TreeNode>que1=new LinkedList<>();
Queue<TreeNode>que2=new LinkedList<>();
que1.add(root);
while(!que1.isEmpty()||!que2.isEmpty()){
if(!que1.isEmpty()){
boolean isFirst=true;//用来标记是否是下一层的第一个节点
while(!que1.isEmpty()){ //当一层遍历完了,再遍历下一层。
TreeNode node=que1.poll();
if(node.left!=null){
if(isFirst){
isFirst=false;
leftMost=node.left;
}
que2.add(node.left);
}
if(node.right!=null){
if(isFirst){
isFirst=false;
leftMost=node.right;
}
que2.add(node.right);
}
}
}
else if(!que2.isEmpty()){
boolean isFirst=true;
while(!que2.isEmpty()){
TreeNode node=que2.poll();
if(node.left!=null){
if(isFirst){
isFirst=false;
leftMost=node.left;
}
que1.add(node.left);
}
if(node.right!=null){
if(isFirst){
isFirst=false;
leftMost=node.right;
}
que1.add(node.right);
}
}
}
}
return leftMost.val;
}
}
LeetCode赛题----Find Left Most Element的更多相关文章
- LeetCode赛题515----Find Largest Element in Each Row
问题描述 You need to find the largest element in each row of a Binary Tree. Example: Input: 1 / \ 2 3 / ...
- 【leetcode刷题笔记】Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode赛题395----Longest Substring with At Least K Repeating Characters
395. Longest Substring with At least K Repeating Characters Find the length of the longest substring ...
- LeetCode赛题394----Decode String
394. Decode String Given an encoded string, return it's decoded string. The encoding rule is: k[enco ...
- LeetCode赛题393----UTF-8 Validation
393. UTF-8 Validation A character in UTF8 can be from 1 to 4 bytes long, subjected to the following ...
- LeetCode赛题392---- Is Subsequence
392. Is Subsequence Given a string s and a string t, check if s is subsequence of t. You may assume ...
- LeetCode赛题391----Perfect Rectangle
#391. Perfect Rectangle Given N axis-aligned rectangles where N > 0, determine if they all togeth ...
- LeetCode赛题390----Elimination Game
# 390. Elimination Game There is a list of sorted integers from 1 to n. Starting from left to right, ...
- 乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array
乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array 一.前言 这次我们还是要改造二分搜索,但是想法却 ...
随机推荐
- DataTables复杂表头
工作上的需要,要做一个复杂的表头的DataTables thead如下 遇到的问题(详细问题可以浏览官网的答案 链接) 需自定义表头(thead),如果不自定义则会 Cannot read prope ...
- 【App性能监控】:Android studio环境的搭建(以及遇到个各种坑)
今天搭建app性能测试环境,使用的是android studio的Android Device Monitor抓取trace日志分析: 1,下载最新的android studio安装,这一步没啥问题: ...
- curl 命令大全
post json curl -H "Content-Type: application/json" -X POST --data '{"userID":100 ...
- 找到MySQL配置文件默认路径
如果不知道当前使用的配置文件的路径,可以尝试下面的操作: # which mysqld /usr/local/mysql/bin/mysqld # /usr/local/mysql/bin/mysql ...
- JDK中ClassLoader的分类以及ClassLoader间的层次关系
几个常见的ClassLoader: bootstrap class loader: 最早启动的class loader,一般使用C语言,汇编语言,或是c++写的,用操作系统本地语言写的.这个cl ...
- ubuntu下安装h2数据库
1.下载h2数据库安装包 http://www.h2database.com/html/download.html 2.解压安装文件包到指定目录 3.运行sh文件 4.访问web地址: http:// ...
- sessionKey/tokenKey
移动端维持登录状态的机制 1. sessionKey/tokenKey哪里来? 1. 登录成功之后,后台返回. 2. sessionKey/tokenKey生成有什么规则? 1. 后台返回的,按照一定 ...
- 聊一聊Iterable与Iterator的那些事!
前言 欢迎关注公众号:Coder编程 获取最新原创技术文章和相关免费学习资料,随时随地学习技术知识! 在上一篇文章通过面试题,让我们来了解Collection,我们会发现Collection接口之上还 ...
- Ubuntu 10.10, 11.04, 11.10这三个版本无法从优盘启动
问题:Ubuntu 10.10, 11.04, 11.10这三个版本无法从优盘启动 解决:从U盘启动安装的时候,会卡住不动.搞定办法相当简单,修改syslinux/syslinuxfg文件:将defa ...
- Swift函数_默认参数
swift中提供了默认参数功能,在声明函数时给参数指定默认值. 例: func inputStudentInfo(name:String,age:Int="26") { print ...