问题描述

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的更多相关文章

  1. 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 / ...

  2. 【leetcode刷题笔记】Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  3. 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 ...

  4. LeetCode赛题394----Decode String

    394. Decode String Given an encoded string, return it's decoded string. The encoding rule is: k[enco ...

  5. 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 ...

  6. 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 ...

  7. LeetCode赛题391----Perfect Rectangle

    #391. Perfect Rectangle Given N axis-aligned rectangles where N > 0, determine if they all togeth ...

  8. LeetCode赛题390----Elimination Game

    # 390. Elimination Game There is a list of sorted integers from 1 to n. Starting from left to right, ...

  9. 乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array

    乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array 一.前言 这次我们还是要改造二分搜索,但是想法却 ...

随机推荐

  1. [Alpha]Scrum Meeting#7

    github 本次会议项目由PM召开,时间为4月9日晚上10点30分 时长15分钟 任务表格 人员 昨日工作 下一步工作 木鬼 整理并发布之前因为清明耽误的博客 撰写每日例会报告 SiMrua 添加暂 ...

  2. ArrayList分析

    ArrayList概述 ArrayList继承了AbstractList,实现了List接口,底层基于动态数组,容量大小可以动态变化,ArrayList中可以添加null元素,另外,ArrayList ...

  3. testng多线程

    1.设置多线程,同一浏览器运行两个用例,但是有其中有一个运行较慢

  4. 1、 小白带你入坑xamarin系列之环境搭建和准备

    重点提示 由于xamarin发展更新很快 目前教程部分内容已经过时 请注意下载最新版本   2018.05.23 www.xamarin.com 1. 小白带你入坑xamarin系列之环境搭建和准备 ...

  5. wusir FTP与HTTP文件传输之TCP Packet解析

    向服务器传输文件(上传或下载)时,FTP与HTTP是两种最常用的应用层协议,这两个协议都是基于TCP协议之上.如果深入到数据包内(Packet)进行查看时,FTP与HTTP进行文件传输时有什么特征代码 ...

  6. javascript中prototype与__proto__

    1.prototype:构造函数独有的属性: __proto__:每个对象都有一个名为__proto__的属性: 注意:每个构造函数(自带与自创)都有一个prototype的属性,构造函数的proto ...

  7. 【Kafka】Kafka数据可靠性深度解读

    转帖:http://www.infoq.com/cn/articles/depth-interpretation-of-kafka-data-reliability Kafka起初是由LinkedIn ...

  8. 数据库命令行操作语句 linux 详细

    1.连接数据库 命令: use <数据库名> 2.查看表的引擎类型等状态信息 SHOW TABLE STATUS [FROMdb_name] [LIKE 'pattern'] 3.当前数据 ...

  9. 九: 操作提示(js版本)

    一.toast 消息提示框 他用到了一个wx.showToast(object) 这样一个方法.作用是显示提示框. /* ---page/test/test.wxml----*/   <butt ...

  10. The following control could not be licensed: TXTextControl.TextControl。。解决方案

    在这篇博客中,下面的控制不能授权:txtextcontrol.textcontrol这意味着,找不到合适的许可证来验证控制.一般情况下,许可证将被自动纳入应用程序,通常不必担心许可证的所有. “许可证 ...