问题描述

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. TortoiseGit-2.0.0.0-64bit问题

    使用TortoiseGit拉取一个项目时,提示: disconnected no supported authentication methods available(server sent: pub ...

  2. Docker安装MongoDb

    1.下载镜像 docker pull mongo 2.运行mongo docker run --name mongo -v /data/mongodb:/data/db -p : -d mongo - ...

  3. Visual Studio性能计数器,负载测试结果分析- Part III

    对于一个多用户的应用程序,性能是非常重要的.性能不仅是执行的速度,它包括负载和并发方面.Visual Studio是可以用于性能测试的工具之一.Visual Studio Test版或Visual S ...

  4. 在Azuer创建自己的Linux_VM

    ---恢复内容开始--- emm..就是想搭个自己的VPN去YouTube看看视屏找找资源什么的... (滑稽.jpg)然后发现似乎需要这个玩意儿 先去申请一个Azuer账户 然后根据要求一步步来就好 ...

  5. Error: Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting

    解决方法: find / -name supervisor.sock unlink /name/supervisor.sock 2. www-data 用户是干什么用的 3.如何通过superviso ...

  6. 一段自用javascript代码

    function jsontoarray(mjson) { var arr = []; ; for(var x in mjson.data){ arr[i] = new Array(); arr[i] ...

  7. WCF系列教程之客户端异步调用服务

    本文参考自http://www.cnblogs.com/wangweimutou/p/4409227.html,纯属读书笔记,加深记忆 一.简介 在前面的随笔中,详细的介绍了WCF客户端服务的调用方法 ...

  8. struts2上传单个文件

    项目目录: struts.xml配置: <constant name="struts.enable.DynamicMethodInvocation" value=" ...

  9. UBUNTU 无法解析域名 解决方法

    莫名其妙的,ubuntu 无法访问外网. ping www.taobao.com  www.baidu.com 提示无效的主机名 但是ping 外网ip 都是可以PING通的. 所以断定是 dns问题 ...

  10. 错误:‘lock_guard’ 在此作用域中尚未声明

    解决:修改报错文件,加入#include <boost/thread/lock_guard.hpp>