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 一.前言 这次我们还是要改造二分搜索,但是想法却 ...
随机推荐
- js中数组的操作方法
今天给大家带来一篇有关数组操作方法的文章. 新建数组 方法一:通过new运算符创建一个数组构造函数. var arr = new Array(); 方法二:通过方括号直接创建直接量数组. var ar ...
- 28.earch in Rotated Sorted Array(排序旋转数组中查找)
Level: Medium 题目描述: Suppose an array sorted in ascending order is rotated at some pivot unknown to ...
- excel 快速填充所有非连续空白单元格
工作中经常会碰到这样的表,需要把空白的单元格填充完成.变成后图.下面就是方法. 工具/原料 EXCEL 方法/步骤 首先选中你要填充的区域, 按“F5"或者 Ctrl + ...
- Mac服务管理-Launchd(转)
背景: 在Mac下没有像Linux那样有很多的关于init方面的工具,从init的发展历史https://en.wikipedia.org/wiki/Init上可以知道,Mac使用的是Launchd作 ...
- SpringMVC 过滤器
参考: http://qq-22530757.iteye.com/blog/2177513 http://www.jdon.com/dl/best/spring-security.html https ...
- RabbitMQ的安装和配置化可视界面
RabbitMQ在windows下的安装 RabbitMQ 它依赖于Erlang,在window上安装时,需要先安装Erlang. 首先确定你的window电脑是32位还是64位,然后下载对应版本的E ...
- resotreIpAddress
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- STL 排序(转载)
这篇文章关于STL中的排序写的虽不深入,但是还是挺好的. 1.sort sort有两种形式,第一种形式有两个迭代器参数,构成一个前开后闭的区间,按照元素的 less 关系排序:第二种形式多加一个指定排 ...
- 大数据sql引擎
Hive:把sql解析后用MapReduce跑 SparkSQL:把sql解析后用Spark跑,比hive快点 Phoenix:一个绕过了MapReduce运行在HBase上的SQL框架 Drill/ ...
- laravel5.4学习--laravel目录结构
Laravel目录结构分析 app目录:主要是存放自己开发的应用代码(里面主要书写 控制器和模型和路由文件) bootstrap目录:laravel启动目录 config目录:主要是存放配置文件信息 ...