问题描述

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. Eclipse 的SVN 插件

    Eclipse 的SVN 插件 简介  Subversive Eclipse 团队开发的SVN 插件. Subclipse Apache 的SVN 团队开发的Eclipse 插件.   Subvers ...

  2. #.NET# DataGrid显示大量数据——DataGridView虚模式

    要解决的目标:如何让 Datagridview 快速平滑显示大量数据 通常,Winform 下的表格控件是很"低效"的,如 DataGrid 和 DataGridView.造成低效 ...

  3. windows server 2012 valid key

    好吧,网页三剑客. 1, load disc iso 2,check ip settings, 3,net-inst-server-start 4,power Node, F2 4.1 F7 usbc ...

  4. ator自动生成mybatis配置和类信息

    generator自动生成mybatis的xml配置.model.map等信息: 1.下载mybatis-generator-core-1.3.2.jar包.        网址:http://cod ...

  5. (转)Linux下部署tomcat及tomcat war包应用程序

    原文:http://www.cnblogs.com/smallfa/news/2017/07/17/7193620.html 1, 通过winscp将tomcat包(6和7版本都是一样的安装方法)和j ...

  6. Java Struts(文件下载)

    1.从注册成功页面跳转至用户详情页面(跳转至UserListAction) 2.UserListAction调用service获得用户列表,并将这些数据传送到UserList.jsp中,UserLis ...

  7. λ(lambda)表达式

    理论阶段 函数接口 函数接口是行为的抽象: 函数接口是数据转换器; java.util.Function包.定义了四个最基础的函数接口: Supplier<T>: 数据提供器,可以提供 T ...

  8. 利用keepalived构建高可用MySQL-HA

    关于MySQL-HA,目前有多种解决方案,比如heartbeat.drbd.mmm.共享存储,但是它们各有优缺点.heartbeat.drbd配置较为复杂,需要自己写脚本才能实现MySQL自动切换,对 ...

  9. Oracle JDBC 连接方式

    格式一:  Oracle JDBC Thin using a ServiceName jdbc:oracle:thin:@//<host>:<port>/<service ...

  10. pureMVC与strangeIoc框架对比

    前言 最近有机会了解到了StrangeIoc框架,就拿来跟自己比较熟悉的pureMVC进行一下简要的对比.这两套开源框架都是基于MVC模式的扩展,pureMVC是一个跨平台跨语言的MVC轻量级应用框架 ...