Maximum Depth of Binary Tree

Question

Problem Statement

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.

Example

Given a binary tree as follow:

  1. 1
  2. / \
  3. 2 3
  4. / \
  5. 4 5

The maximum depth is 3.

题解 - 递归

树遍历的题最方便的写法自然是递归,不过递归调用的层数过多可能会导致栈空间溢出,因此需要适当考虑递归调用的层数。我们首先来看看使用递归如何解这道题,要求二叉树的最大深度,直观上来讲使用深度优先搜索判断左右子树的深度孰大孰小即可,从根节点往下一层树的深度即自增1,遇到NULL时即返回0。

由于对每个节点都会使用一次maxDepth,故时间复杂度为 O(n), 树的深度最大为 n, 最小为 \log_2 n, 故空间复杂度介于 O(\log n)O(n) 之间。

C++

  1. /**
  2. * Definition of TreeNode:
  3. * class TreeNode {
  4. * public:
  5. * int val;
  6. * TreeNode *left, *right;
  7. * TreeNode(int val) {
  8. * this->val = val;
  9. * this->left = this->right = NULL;
  10. * }
  11. * }
  12. */
  13. class Solution {
  14. public:
  15. /**
  16. * @param root: The root of binary tree.
  17. * @return: An integer
  18. */
  19. int maxDepth(TreeNode *root) {
  20. if (NULL == root) {
  21. return 0;
  22. }
  23. int left_depth = maxDepth(root->left);
  24. int right_depth = maxDepth(root->right);
  25. return max(left_depth, right_depth) + 1;
  26. }
  27. };

Java

  1. /**
  2. * Definition of TreeNode:
  3. * public class TreeNode {
  4. * public int val;
  5. * public TreeNode left, right;
  6. * public TreeNode(int val) {
  7. * this.val = val;
  8. * this.left = this.right = null;
  9. * }
  10. * }
  11. */
  12. public class Solution {
  13. /**
  14. * @param root: The root of binary tree.
  15. * @return: An integer.
  16. */
  17. public int maxDepth(TreeNode root) {
  18. // write your code here
  19. if (root == null) {
  20. return 0;
  21. }
  22. return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
  23. }
  24. }

题解 - 迭代(显式栈)

使用递归可能会导致栈空间溢出,这里使用显式栈空间(使用堆内存)来代替之前的隐式栈空间。从上节递归版的代码(先处理左子树,后处理右子树,最后返回其中的较大值)来看,是可以使用类似后序遍历的迭代思想去实现的。

首先使用后序遍历的模板,在每次迭代循环结束处比较栈当前的大小和当前最大值max_depth进行比较。

C++

  1. /**
  2. * Definition of TreeNode:
  3. * class TreeNode {
  4. * public:
  5. * int val;
  6. * TreeNode *left, *right;
  7. * TreeNode(int val) {
  8. * this->val = val;
  9. * this->left = this->right = NULL;
  10. * }
  11. * }
  12. */
  13. class Solution {
  14. public:
  15. /**
  16. * @param root: The root of binary tree.
  17. * @return: An integer
  18. */
  19. int maxDepth(TreeNode *root) {
  20. if (NULL == root) {
  21. return 0;
  22. }
  23. TreeNode *curr = NULL, *prev = NULL;
  24. stack<TreeNode *> s;
  25. s.push(root);
  26. int max_depth = 0;
  27. while(!s.empty()) {
  28. curr = s.top();
  29. if (!prev || prev->left == curr || prev->right == curr) {
  30. if (curr->left) {
  31. s.push(curr->left);
  32. } else if (curr->right){
  33. s.push(curr->right);
  34. }
  35. } else if (curr->left == prev) {
  36. if (curr->right) {
  37. s.push(curr->right);
  38. }
  39. } else {
  40. s.pop();
  41. }
  42. prev = curr;
  43. if (s.size() > max_depth) {
  44. max_depth = s.size();
  45. }
  46. }
  47. return max_depth;
  48. }
  49. };

题解3 - 迭代(队列)

在使用了递归/后序遍历求解树最大深度之后,我们还可以直接从问题出发进行分析,树的最大深度即为广度优先搜索中的层数,故可以直接使用广度优先搜索求出最大深度。

C++

  1. /**
  2. * Definition of TreeNode:
  3. * class TreeNode {
  4. * public:
  5. * int val;
  6. * TreeNode *left, *right;
  7. * TreeNode(int val) {
  8. * this->val = val;
  9. * this->left = this->right = NULL;
  10. * }
  11. * }
  12. */
  13. class Solution {
  14. public:
  15. /**
  16. * @param root: The root of binary tree.
  17. * @return: An integer
  18. */
  19. int maxDepth(TreeNode *root) {
  20. if (NULL == root) {
  21. return 0;
  22. }
  23. queue<TreeNode *> q;
  24. q.push(root);
  25. int max_depth = 0;
  26. while(!q.empty()) {
  27. int size = q.size();
  28. for (int i = 0; i != size; ++i) {
  29. TreeNode *node = q.front();
  30. q.pop();
  31. if (node->left) {
  32. q.push(node->left);
  33. }
  34. if (node->right) {
  35. q.push(node->right);
  36. }
  37. }
  38. ++max_depth;
  39. }
  40. return max_depth;
  41. }
  42. };

Java

  1. /**
  2. * Definition of TreeNode:
  3. * public class TreeNode {
  4. * public int val;
  5. * public TreeNode left, right;
  6. * public TreeNode(int val) {
  7. * this.val = val;
  8. * this.left = this.right = null;
  9. * }
  10. * }
  11. */
  12. public class Solution {
  13. /**
  14. * @param root: The root of binary tree.
  15. * @return: An integer.
  16. */
  17. public int maxDepth(TreeNode root) {
  18. if (root == null) {
  19. return 0;
  20. }
  21. int depth = 0;
  22. Queue<TreeNode> q = new LinkedList<TreeNode>();
  23. q.offer(root);
  24. while (!q.isEmpty()) {
  25. depth++;
  26. int qLen = q.size();
  27. for (int i = 0; i < qLen; i++) {
  28. TreeNode node = q.poll();
  29. if (node.left != null) q.offer(node.left);
  30. if (node.right != null) q.offer(node.right);
  31. }
  32. }
  33. return depth;
  34. }
  35. }

源码分析

广度优先中队列的使用中,qLen 需要在for 循环遍历之前获得,因为它是一个变量。

复杂度分析

最坏情况下空间复杂度为 O(n), 遍历每一个节点,时间复杂度为 O(n),