104. Maximum Depth of Binary Tree
2015-04-18 13:40
363 查看
题目:
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.
Hide Tags
Tree Depth-first Search
链接: http://leetcode.com/problems/maximum-depth-of-binary-tree/
题解:
也是DFS。 假如不使用recursive,可以用level order traversal,使用queue来辅助,每增加一层,depth + 1。 Recursive很简单, 有机会要试试iterative
Time Complexity - O(n), Space Complexity - O(n)。
Update
二刷:
Java:
Recursive:
层序遍历:
三刷:
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.
Hide Tags
Tree Depth-first Search
链接: http://leetcode.com/problems/maximum-depth-of-binary-tree/
题解:
也是DFS。 假如不使用recursive,可以用level order traversal,使用queue来辅助,每增加一层,depth + 1。 Recursive很简单, 有机会要试试iterative
Time Complexity - O(n), Space Complexity - O(n)。
public class Solution { public int maxDepth(TreeNode root) { if(root == null) return 0; return 1 + Math.max(maxDepth(root.left), maxDepth(root.right)); } }
Update
/**
* 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; return 1 + Math.max(maxDepth(root.left), maxDepth(root.right)); } }
二刷:
Java:
Recursive:
/** * 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; } return 1 + Math.max(maxDepth(root.left), maxDepth(root.right)); } }
层序遍历:
/** * 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; } Queue<TreeNode> q = new LinkedList<>(); q.offer(root); int curLevel = 1; int nextLevel = 0; int depth = 0; while (!q.isEmpty()) { TreeNode node = q.poll(); curLevel--; if (node.left != null) { q.offer(node.left); nextLevel++; } if (node.right != null) { q.offer(node.right); nextLevel++; } if (curLevel == 0) { curLevel = nextLevel; nextLevel = 0; depth++; } } return depth; } }
三刷:
/** * 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; } return 1 + Math.max(maxDepth(root.left), maxDepth(root.right)); } }
相关文章推荐
- Binary Tree
- 1156 Binary tree 深优 指针 简单树的遍历
- POJ 2499 Binary Tree(水题)
- LeetCode 111. Minimum Depth of Binary Tree
- 【LeetCode】104.Maximum Depth of Binary Tree
- Invert Binary Tree
- HDU 5573 Binary Tree(找规律)
- leetcode 每日一题 104. Maximum Depth of Binary Tree
- Binary Tree---Subtree
- 渣渣写LEETCODE——104. Maximum Depth of Binary Tree
- LeetCode - 111. Minimum Depth of Binary Tree
- B-tree, Binary Tree, B+Tree, B*-Tree 的概念对比
- Data Structure:Binary Tree
- LeetCode 104. Maximum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- 【LeetCode】104. Maximum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- 【indeed】C - Binary Tree
- LeetCode 226. Invert Binary Tree (反转二叉树)