博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode算法: Find Bottom Left Tree Value
阅读量:4630 次
发布时间:2019-06-09

本文共 1110 字,大约阅读时间需要 3 分钟。

leetcode算法: Find Bottom Left Tree Value Given a binary tree, find the leftmost value 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 (i.e., the given root node) is not NULL. 这道题 是 给我们一颗二叉树的根节点,让我们找到最后一层的最左边的节点。 绞尽脑汁之后,利用二维数组把每个节点和所在层次扒下来了,但是还是太麻烦。看了其他大神的思路豁然开朗! 大神的思路是:从右向左广度优先遍历!! 最后一个节点就是我们要的结果!! 献上我的代码:
1 # Definition for a binary tree node. 2 # class TreeNode(object): 3 #     def __init__(self, x): 4 #         self.val = x 5 #         self.left = None 6 #         self.right = None 7  8 class Solution(object): 9     def findBottomLeftValue(self, root):10         """11         :type root: TreeNode12         :rtype: int13         """14         q = [root]15         while q:16             node = q.pop(0)17             if node.right is not None:18                 q.append(node.right)19             if node.left is not None:20                 q.append(node.left)21         return node.val

 

转载于:https://www.cnblogs.com/Lin-Yi/p/7501620.html

你可能感兴趣的文章
对类型“DevExpress.Xpf.Grid.GridControl”的构造函数执行符合指定的绑定约束的调用时引发了异常。...
查看>>
dogse入门指南
查看>>
Spring 整合quartz 时 定时任务被调用两次以及quartz 的配置
查看>>
oracle测试环境表空间清理
查看>>
余额宝技术架构读后感
查看>>
1.lamp网站构建
查看>>
狼人杀
查看>>
《lua程序设计(第二版)》学习笔记(五)-- 函数基础
查看>>
【CF EDU59 E】 Vasya and Binary String (DP)
查看>>
Catel(翻译)-为什么选择Catel
查看>>
angular轮播图
查看>>
指针小白:修改*p与p会对相应的地址的变量产生什么影响?各个变量指针的长度为多少?...
查看>>
文本相关CSS
查看>>
全新的开始
查看>>
leaflet地图框架
查看>>
mybatis的一些基础问题
查看>>
封装、继承、多态
查看>>
visual webgui theme designer
查看>>
【制作镜像】BCEC制作镜像
查看>>
Hadoop学习笔记之三 数据流向
查看>>