【LeetCode】Maximum Depth of Binary Tree && Minimum Depth of Binary Tree
来源:互联网 发布:威戈背包知乎 编辑:程序博客网 时间:2024/06/07 15:43
1、Maximum Depth of Binary Tree
Total Accepted: 10889 Total Submissions: 24617 My Submissions
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Have you been asked this question in an interview? Yes
Discuss
2、Minimum Depth of Binary Tree
Total Accepted: 8209 Total Submissions: 28728 My Submissions
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.
Have you been asked this question in an interview? Yes
Discuss
min和max其实不大一样。
max到叶子节点,最长路程,其实是一直深度搜索的过程。
min,需要判断当前是否为叶子节点,这个很重要。
Total Accepted: 10889 Total Submissions: 24617 My Submissions
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Have you been asked this question in an interview? Yes
Discuss
2、Minimum Depth of Binary Tree
Total Accepted: 8209 Total Submissions: 28728 My Submissions
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.
Have you been asked this question in an interview? Yes
Discuss
min和max其实不大一样。
max到叶子节点,最长路程,其实是一直深度搜索的过程。
min,需要判断当前是否为叶子节点,这个很重要。
MAX Java AC
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public int maxDeep; public int maxDepth(TreeNode root) { if(root == null){ return 0; } maxDeep = 0; dfs(root, 1); return maxDeep; } public void dfs(TreeNode root, int deep){ if(root == null){ return; } maxDeep = Math.max(maxDeep, deep); dfs(root.left, deep+1); dfs(root.right, deep+1); }}
MIN Java AC
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public int minDeep; public int minDepth(TreeNode root) { if(root == null){ return 0; } minDeep = Integer.MAX_VALUE; dfs(root, 1); return minDeep; } public void dfs(TreeNode root, int deep){ if(root.left == null && root.right == null){ minDeep = Math.min(minDeep, deep); return; } if(root.left != null){ dfs(root.left, deep+1); } if(root.right != null){ dfs(root.right, deep+1); } }}
0 0
- [Leetcode] Maximum |Minimum Depth of Binary Tree
- LeetCode-Maximum/Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree,Maximum Depth of Binary Tree
- 【LeetCode】Maximum Depth of Binary Tree && Minimum Depth of Binary Tree
- leetcode-Minimum Depth of Binary Tree &&Maximum Depth of Binary Tree
- LeetCode:Minimum Depth of Binary Tree&& Maximum Depth of Binary Tree
- LeetCode 104. Maximum Depth of Binary Tree和111. Minimum Depth of Binary Tree 递归
- Minimum(Maximum) Depth of Binary Tree
- Maximum & Minimum Depth of Binary Tree
- 111. Minimum & Maximum Depth of Binary Tree
- Leetcode - Tree - Maximum Depth of Binary Tree
- [leetcode][tree] Maximum Depth of Binary Tree
- [Leetcode 104, Easy] Minimum (Maximum) Depth of Binary Tree
- <LeetCode OJ> (104 / 111) Maximum & Minimum Depth of Binary Tree
- Leetcode 111,104. Minimum/Maximum Depth of Binary Tree
- LeetCode 104. Maximum Depth of Binary Tree && Minimum Depth of Binary
- LeetCode: Minimum Depth of Binary Tree
- [Leetcode] Minimum Depth of Binary Tree
- android 4.0与android 2.3的变化
- 删除SVN用户信息
- Java Socket网络编程常见异常
- 输入两个整数 n 和 m,从数列1,2,3…….n 中随意取几个数,使其和等于 m ,要求将其中所有的可能组合列出来
- 第二章 身份验证
- 【LeetCode】Maximum Depth of Binary Tree && Minimum Depth of Binary Tree
- Java 可变参数
- Android使用JNI
- 获取设备环境句柄的方法
- Hibernate之Query查询的list()方法和iterator()方法的区别
- jquery serializeObject() 读取
- django+ajaxgold
- CPU的常见故障及处理方法
- HDU-1482-Counterfeit Dollar