[Swift]LeetCode513. 找树左下角的值 | Find Bottom Left Tree Value
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10392509.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Given a binary tree, find the leftmost value in the last row of the tree.
Example 1:
Input:2/ \1 3Output:1
Example 2:
Input:1/ \2 3/ / \4 5 6/7Output:7
Note: You may assume the tree (i.e., the given root node) is not NULL.
给定一个二叉树,在树的最后一行找到最左边的值。
示例 1:
输入:2/ \1 3输出:1
示例 2:
输入:1/ \2 3/ / \4 5 6/7输出:7
注意: 您可以假设树(即给定的根节点)不为 NULL。
56ms
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * public var val: Int 5 * public var left: TreeNode? 6 * public var right: TreeNode? 7 * public init(_ val: Int) { 8 * self.val = val 9 * self.left = nil 10 * self.right = nil 11 * } 12 * } 13 */ 14 class Solution { 15 func findBottomLeftValue(_ root: TreeNode?) -> Int { 16 guard let node = root else { 17 return 0 18 } 19 20 var currentLevel = 0 21 var currentValue = node.val 22 23 func DFS(_ root: TreeNode?, level: Int) { 24 guard let node = root else { 25 return 26 } 27 DFS(node.left, level: level + 1) 28 if level > currentLevel { 29 currentLevel = level 30 currentValue = node.val 31 } 32 DFS(node.right, level: level + 1) 33 } 34 DFS(node, level: 0) 35 return currentValue 36 } 37 }
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * public var val: Int 5 * public var left: TreeNode? 6 * public var right: TreeNode? 7 * public init(_ val: Int) { 8 * self.val = val 9 * self.left = nil 10 * self.right = nil 11 * } 12 * } 13 */ 14 class Solution { 15 func findBottomLeftValue(_ root: TreeNode?) -> Int { 16 var tree = [TreeNode]() 17 tree.append(root!) 18 var node = root 19 while !tree.isEmpty { 20 node = tree.removeFirst() 21 if node?.right != nil { 22 tree.append(node!.right!) 23 } 24 if node?.left != nil { 25 tree.append(node!.left!) 26 } 27 } 28 return node!.val 29 } 30 }
64ms
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * public var val: Int 5 * public var left: TreeNode? 6 * public var right: TreeNode? 7 * public init(_ val: Int) { 8 * self.val = val 9 * self.left = nil 10 * self.right = nil 11 * } 12 * } 13 */ 14 class Solution { 15 func findBottomLeftValue(_ root: TreeNode?) -> Int { 16 17 var queue = [TreeNode]() 18 19 queue.append(root!) 20 var result = 0 21 var level = [Int]() 22 23 while !queue.isEmpty { 24 25 let size = queue.count 26 level = [] 27 for i in 0 ..< size { 28 29 let node = queue.removeFirst() 30 level.append(node.val) 31 if node.left != nil{ 32 queue.append(node.left!) 33 } 34 35 if node.right != nil{ 36 queue.append(node.right!) 37 } 38 } 39 result = level[0] 40 } 41 42 return result 43 } 44 }
68ms
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * public var val: Int 5 * public var left: TreeNode? 6 * public var right: TreeNode? 7 * public init(_ val: Int) { 8 * self.val = val 9 * self.left = nil 10 * self.right = nil 11 * } 12 * } 13 */ 14 class Solution { 15 func findBottomLeftValue(_ root: TreeNode?) -> Int { 16 guard let root = root else { return 0} 17 18 var value = (root.val, 0) 19 20 if let leftNode = root.left { 21 value = helper(node: leftNode, level: 1) 22 } 23 24 if let rightNode = root.right { 25 let value2 = helper(node: rightNode, level: 1) 26 if value2.1 > value.1 { 27 value = value2 28 } 29 } 30 31 return value.0 32 } 33 34 func helper(node: TreeNode, level: Int) -> (Int, Int) { 35 36 var value = (node.val, level) 37 38 if let leftNode = node.left { 39 value = helper(node: leftNode, level: level + 1) 40 } 41 42 if let rightNode = node.right { 43 let value2 = helper(node: rightNode, level: level + 1) 44 if value2.1 > value.1 { 45 value = value2 46 } 47 } 48 return value 49 } 50 }
84ms
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * public var val: Int 5 * public var left: TreeNode? 6 * public var right: TreeNode? 7 * public init(_ val: Int) { 8 * self.val = val 9 * self.left = nil 10 * self.right = nil 11 * } 12 * } 13 */ 14 class Solution { 15 func findBottomLeftValue(_ root: TreeNode?) -> Int { 16 return helper(root, 0)!.1 17 } 18 19 func helper(_ root: TreeNode?, _ level: Int) -> (Int, Int)? { 20 guard let root = root else { 21 return nil 22 } 23 24 let a = helper(root.left, level + 1) 25 let b = helper(root.right, level + 1) 26 if a == nil && b == nil { 27 return (level, root.val) 28 } 29 if a != nil && b != nil { 30 if a!.0 >= b!.0 { 31 return a 32 } else { 33 return b 34 } 35 } 36 return a ?? b 37 } 38 }
88ms
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * public var val: Int 5 * public var left: TreeNode? 6 * public var right: TreeNode? 7 * public init(_ val: Int) { 8 * self.val = val 9 * self.left = nil 10 * self.right = nil 11 * } 12 * } 13 */ 14 15 class Queue<Type> { 16 var array: [Type] = [] 17 var index = -1 18 19 init() { 20 } 21 22 func insert(_ T: Type) { 23 index = index + 1 24 array.append(T) 25 } 26 27 func delete() -> Type? { 28 if (index == -1) { 29 return nil 30 } 31 32 let ret = array[0] 33 array.remove(at: 0) 34 index = index - 1 35 36 return ret 37 } 38 39 func front() -> Type? { 40 if (index == -1) { 41 return nil 42 } 43 44 let ret = array[0] 45 return ret 46 } 47 48 func isEmpty() -> Bool { 49 return index == -1 50 } 51 } 52 53 class Solution { 54 func findBottomLeftValue(_ root: TreeNode?) -> Int { 55 if (root == nil) { 56 return -1 57 } 58 59 var result = -1 60 var q1 = Queue<TreeNode>() 61 var q2 = Queue<TreeNode>() 62 var temp: Queue<TreeNode> = q1 63 var level = 0 64 var stop = false 65 66 q1.insert(root!) 67 68 while (false == stop) { 69 70 stop = true 71 72 if (false == q1.isEmpty()) { 73 result = q1.front()!.val 74 } 75 76 while(false == q1.isEmpty()) { 77 let node = q1.delete() 78 if (node!.left != nil) { 79 q2.insert(node!.left!) 80 } 81 if (node!.right != nil) { 82 q2.insert(node!.right!) 83 } 84 stop = false 85 } 86 87 temp = q1 88 q1 = q2 89 q2 = temp 90 } 91 92 return result 93 } 94 }
92ms
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * public var val: Int 5 * public var left: TreeNode? 6 * public var right: TreeNode? 7 * public init(_ val: Int) { 8 * self.val = val 9 * self.left = nil 10 * self.right = nil 11 * } 12 * } 13 */ 14 class Solution { 15 var ansFi = Int.min 16 17 var level = -1 18 19 func findBottomLeftValue(_ root: TreeNode?) -> Int { 20 21 22 findBottomHelper(root: root, currentLevel: 0) 23 24 return ansFi 25 } 26 27 private func findBottomHelper(root: TreeNode?, currentLevel: Int) { 28 29 guard let root = root else { 30 return 31 } 32 33 if level+1 == currentLevel { 34 ansFi = root.val 35 level = currentLevel 36 } 37 38 findBottomHelper(root: root.left, currentLevel: currentLevel + 1) 39 40 findBottomHelper(root: root.right, currentLevel: currentLevel + 1) 41 } 42 }
转载于:https://www.cnblogs.com/strengthen/p/10392509.html
[Swift]LeetCode513. 找树左下角的值 | Find Bottom Left Tree Value相关推荐
- leetcode513. 找树左下角的值(dfs)
给定一个二叉树,在树的最后一行找到最左边的值. 代码 /*** Definition for a binary tree node.* public class TreeNode {* int val ...
- 代码随想录第18天|找树左下角的值,路径总和,从中序和后序遍历序列构造二叉树
LeetCode513.找树左下角的值 题目链接:513. 找树左下角的值 - 力扣(LeetCode) 思路: 迭代法(只需要记录最后一行第一个节点的数值就可以了.): /*** Definitio ...
- Java实现 LeetCode 513 找树左下角的值
513. 找树左下角的值 给定一个二叉树,在树的最后一行找到最左边的值. 示例 1: 输入: 2/ \1 3 输出: 1 示例 2: 输入: 1/ \2 3/ / \ 4 5 6/7 输出: 7 注意 ...
- 找树左下角的值+路径总和+从前序和中序遍历序列构造二叉树(day18*)
这篇可以主要关注一下如何确定递归时是否需要返回值. LC513. 找树左下角的值 给定一个二叉树的根节点,请找出该二叉树的 最底层最左边 节点的值. 思路1 层序遍历 class Solution:d ...
- Suzy找到实习了吗Day 18 | 二叉树进行中:513 找树左下角的值,112 路径总和 ,106.从中序与后序遍历序列构造二叉树
513 找树左下角的值 solution # Definition for a binary tree node. # class TreeNode: # def __init__(self, val ...
- LeetCode 513. 找树左下角的值 思考分析
题目 给定一个二叉树,在树的最后一行找到最左边的值. 递归解 左下角要满足两个条件: 1.深度最大的叶子结点 2.最左结点:使用前序遍历,优先左边搜索. 1.确定递归函数的参数和返回值 参数:树的根结 ...
- LeetCode 513. 找树左下角的值(按层遍历 queue)
1. 题目 给定一个二叉树,在树的最后一行找到最左边的值. 2. 解题 利用队列按层次遍历 顺序,根右左,要求最左边的一个,所以根右左,最后一个队列元素就是答案 class Solution {pub ...
- leetcode —— 513. 找树左下角的值
给定一个二叉树,在树的最后一行找到最左边的值. 示例 1: 示例 2: 解题思路:使用广度优先遍历,因为题目要求寻找的是最底层的最左边的节点.因此我们维护一个变量--节点所在的树的高度,设根节点的高度 ...
- LeetCode 513. 找树左下角的值(递归)
题目描述 给定一个二叉树,在树的最后一行找到最左边的值. 思路 详见链接 代码 class Solution:def findBottomLeftValue(self,root:TreeNode) - ...
最新文章
- 200多位专家热议“智慧城市” 建议尽快完善标准体系
- Cisco WLAN 控制器的配置
- python学习笔记 --- 随机数进阶
- java 线程 wait 一定要同步_java中使用wait就得使用同步锁,而且2个线程必须都使用同步代码块,否则就会异常...
- 安装python的pip模块
- nil 作比较时应该加上双引号
- MATLAB获取系统时间
- Linux查看文件第几行到第几行命令
- 听指令的小方块(一)
- 数据库表的建立与基本操作
- CAS4搭建HTTP环境
- Gerrit 安装lfs插件
- Laravel查询构造器的pluck方法第一个参数可选类型array的问题
- 电脑读卡器,读卡器是什么
- 《读九章算术学Python》如何用Python编程实现盈不足术?附图解分析、代码实现和习题解答
- 我的AI转型之路与AI之我见
- ZYNQ之FPGA 片内RAM读写测试实验
- mysql数据库 笔试题
- oracle异构迁移mysql方案实施(含原理)——已迁移成功
- Qt字符转换、文件操作、加密、电脑操作