LeetCode oj 104. Maximum Depth of Binary Tree(DFS||BFS)
来源:互联网 发布:快搜 知乎 编辑:程序博客网 时间:2024/06/08 11:47
104. Maximum Depth of Binary Tree
- Total Accepted: 179819
- Total Submissions: 361510
- Difficulty: Easy
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.
Subscribe to see which companies asked this question
求二叉树的深度
题很水,DFS和BFS都可以
BFS:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { int count = 0; public int maxDepth(TreeNode root) { Queue<TreeNode> q = new LinkedList<TreeNode>();if(root == null)return 0;q.clear();q.add(root);while(!q.isEmpty()){count++;int len = q.size();for(int i=0;i<len;i++){TreeNode t = q.poll();if(t.left != null){q.add(t.left);}if(t.right != null){q.add(t.right);}}}return count;}}DFS:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public int maxDepth(TreeNode root) { if(root == null){return 0;}if(root.left == null && root.right == null){return 1;}int Depth_left = maxDepth(root.left);int Depth_right = maxDepth(root.right);return Math.max(Depth_left,Depth_right) + 1;}}
0 0
- LeetCode oj 104. Maximum Depth of Binary Tree(DFS||BFS)
- 【LeetCode】104. Maximum Depth of Binary Tree(DFS|BFS)
- [LeetCode OJ]Maximum Depth of Binary Tree
- LeetCode OJ:Maximum Depth of Binary Tree
- LeetCode OJ - Maximum Depth of Binary Tree
- [LeetCode OJ]Maximum Depth Of Binary Tree
- LeetCode OJ Maximum Depth of Binary Tree
- 【leetcode】104. Maximum Depth of Binary Tree【java】三种实现方法:递归、BFS、DFS
- [DFS] leetcode 104. Maximum Depth of Binary Tree
- LeetCode OJ : 104. Maximum Depth of Binary Tree
- DFS:104. Maximum Depth of Binary Tree
- [LeetCode]104.Maximum Depth of Binary Tree
- 【LeetCode】104.Maximum Depth of Binary Tree
- [Leetcode] 104. Maximum Depth of Binary Tree
- LeetCode --- 104. Maximum Depth of Binary Tree
- LeetCode 104. Maximum Depth of Binary Tree
- leetCode 104. Maximum Depth of Binary Tree
- 104. Maximum Depth of Binary Tree LeetCode
- POJ 2836 Rectangular Covering -状压DP+枚举
- 最大后验概率(MAP)
- 开发之抽象工厂模式
- 2016.9.28
- python数组访问
- LeetCode oj 104. Maximum Depth of Binary Tree(DFS||BFS)
- Hard 57题 Insert Interval
- how to make Completion Handlers and use it in Swift
- Win10系统下Genymotion不能拖曳安装应用的问题
- 移动端笔记系列1--认识移动端的开发
- ArchLinux编译qfs
- 地方鬼地方好地方
- Design Pattern: General Idea of Program Design
- FileUploadExample