leetcode 111. Minimum Depth of Binary Tree(week 15)
来源:互联网 发布:手机淘宝怎么改价格 编辑:程序博客网 时间:2024/06/06 07:36
题目链接:https://leetcode.com/problems/minimum-depth-of-binary-tree/#/description
这道题目求一棵树的最小深度的问题,这次我们用非递归的方法
经验(非递归方法):
初始化栈(root,当前高度): while(栈非空): node = 弹出栈 如果node非空 如果该节点为尾节点: 更新最小高度 左节点压入堆栈(node.left, 当前高度) 右节点压入堆栈(node.right, 当前高度)
stack = [(root, 0)]min = 10000while(stack): node, current = stack.pop() if(node): if(not node.left and not node.right and current<min): min = current + 1 stack.append((node.left, current + 1)) stack.append((node.right, current + 1))
class Solution(object): def minDepth(self, root): """ :type root: TreeNode :rtype: int """ if(not root): return 0 else: return self.dfs(root) def dfs(self, root): stack = [(root, 0)] min = 10000 while(stack): node, current = stack.pop() print(current) if(node): if(not node.left and not node.right and current<min): min = current + 1 stack.append((node.left, current + 1)) stack.append((node.right, current + 1)) return min
阅读全文
0 0
- leetcode 111. Minimum Depth of Binary Tree(week 15)
- LeetCode 111. Minimum Depth of Binary Tree
- [LeetCode]111.Minimum Depth of Binary Tree
- 【LeetCode】111.Minimum Depth of Binary Tree
- [Leetcode] 111. Minimum Depth of Binary Tree
- [leetcode] 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree LeetCode
- 【LeetCode】111. Minimum Depth of Binary Tree
- LeetCode *** 111. Minimum Depth of Binary Tree
- LeetCode 111. Minimum Depth of Binary Tree
- leetcode-111. Minimum Depth of Binary Tree
- LeetCode-111.Minimum Depth of Binary Tree
- LeetCode 111.Minimum Depth of Binary Tree
- Leetcode 111. Minimum Depth of Binary Tree
- [leetcode]111.Minimum Depth of Binary Tree
- 111.[Leetcode]Minimum Depth of Binary Tree
- LeetCode 111. Minimum Depth of Binary Tree
- 【leetcode】111. Minimum Depth of Binary Tree
- HTTP 传输内容的压缩
- ExoPlayer google 试用exoplayer:r2.3.0
- 从一个git仓库迁移到另外一个git仓库
- BZOJ 1798 [Ahoi2009] Seq 维护序列seq [线段树+多重标记下传]【数据结构】
- 基于等高线算法绘制几何管道,并显示数据
- leetcode 111. Minimum Depth of Binary Tree(week 15)
- 【iTween】单点移动和旋转
- java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 解决方法
- UESTC 1593 老司机破阵 优先队列+双端链表
- 对JavaScript中对象字面量的理解
- HTTP 协议详解与HTTP 协议压缩
- Android官方下拉选择控件Spinner
- HDU 1072 Nightmare(搜索-DFS)
- OpenCV官方文档 理解k