Leetcode_111_Minimum Depth of Binary Tree
来源:互联网 发布:linux ntp时间同步配置 编辑:程序博客网 时间:2024/06/11 23:32
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41964249
Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
思路:
(1)题意为求解二叉树最小深度:即为从树根到叶子节点最短路径。
(2)这道题的思路和按层次输出二叉树类似,按层次输出二叉树可以参照http://blog.csdn.net/pistolove/article/details/41929059。
(3)为了找到最短路径,在对二叉树进行层次遍历过程中,如果遇到第一个叶子节点,则该叶子节点到根节点的距离即为最短路径。所以只需在对二叉树层次遍历的过程中,判断遍历的节点是否有左右孩子节点,如果没有左右孩子节点,则该节点到根节点之间的距离即为最短路径。
(4)如果能够理解二叉树层次遍历的思想,那么相关的很多题目都能从中得到解答。
(5)希望对你有所帮助。谢谢。
算法代码实现如下所示:
//最小深度public static int getUndeep(TreeNode root){if(root==null) return 0;if(root!=null&&root.left==null&&root.right==null) return 1;//根节点不为空 默认为1层int level = 1;LinkedList<TreeNode> list = new LinkedList<TreeNode>();list.add(root);int first = 0;int last = 1;while(first<list.size()){last = list.size();while(first<last){if(list.get(first).left!=null){list.add(list.get(first).left);}if(list.get(first).right!=null){list.add(list.get(first).right);}if(list.get(first).left==null && list.get(first).right==null){return level++;}first++;}level++;}return level;}
1 0
- Leetcode_111_Minimum Depth of Binary Tree
- leetcode_111_Minimum Depth of Binary Tree
- leetcode_111_Minimum Depth of Binary Tree
- Depth of binary tree
- Minimum Depth of Binary Tree,Maximum Depth of Binary Tree
- LeetCode: Depth of Binary Tree
- Maximum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Minimum Depth of Binary Tree
- C语言面向对象编程(二):继承详解
- nyoj-325 zb的生日
- 用C#识别图像中的文字_MODI OCR
- Ubuntu上安装apktool
- C语言面向对象编程(三):虚函数与多态
- Leetcode_111_Minimum Depth of Binary Tree
- 第16周 项目4--为动态数组扩容
- 样式和主题之样式
- shared_ptr:资源管理利器
- ORACLE EBS财务科目FLEX FIELD的添加
- C语言面向对象编程(四):面向接口编程
- Eclipse使用入门(一)
- Ramfs与Tmpfs的使用
- C语言面向对象编程(五):单链表实现