二叉树的最大深度详解

想了解更多数据结构以及算法题,可以关注微信公众号“数据结构和算法”,每天一题为你精彩解答。也可以扫描下面的二维码关注
在这里插入图片描述

问题

给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。

示例:
给定二叉树 [3,9,20,null,null,15,7]

    3
   / \
  9  20
    /  \
   15   7

返回它的最大深度 3 。

数据结构:

java:树节点的数据结构

public class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
        val = x;
    }
}

C语言:树节点的数据结构

struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
};

C++:树节点的数据结构

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

递归写法

我们能想到的最简单的方式估计就是递归了,也就是下面这个图
在这里插入图片描述
如果对递归不熟悉的话可以看下我前面讲的关于复仇一个故事362,汉诺塔。下面我们来画个图来分析下
在这里插入图片描述
看明白了上面的过程,代码就容易多了,我们看下

java

public int maxDepth(TreeNode root) {
    if (root == null)
        return 0;
    return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
}

C语言

int maxDepth(struct TreeNode* root) {
    if (root == NULL)
        return 0;
    return max(maxDepth(root -> left), maxDepth(root -> right)) + 1;
}

int max(int left, int right) {
    return left > right ? left : right;
}

C++

public:
int maxDepth(TreeNode* root) {
    if (root == NULL)
        return 0;
    return max(maxDepth(root -> left), maxDepth(root -> right)) + 1;
}

BFS:

除了递归,我们还可能想到的就是BFS(宽度优先搜索算法(又称广度优先搜索)),他的实现原理就是一层层遍历,统计一下总共有多少层,我们来画个图分析一下。

在这里插入图片描述

一层一层往下走,统计总共有多少层,我们来看下代码

java

 public int maxDepth(TreeNode root) {
     if (root == null)
         return 0;
     Deque<TreeNode> stack = new LinkedList<>();
     stack.push(root);
     int count = 0;
     while (!stack.isEmpty()) {
         int size = stack.size();
         while (size-- > 0) {
            TreeNode cur = stack.pop();
            if (cur.left != null)
                stack.addLast(cur.left);
            if (cur.right != null)
                stack.addLast(cur.right);
        }
        count++;
    }
    return count;
}

C++

 public:
 int maxDepth(TreeNode* root) {
     if (root == NULL)
         return 0;
     int res = 0;
     queue<TreeNode *>q;
     q.push(root);
     while (!q.empty()) {
         ++res;
        for (int i = 0, n = q.size(); i < n; ++i) {
            TreeNode * p = q.front();
            q.pop();
            if (p -> left != NULL)
                q.push(p -> left);
            if (p -> right != NULL)
                q.push(p -> right);
        }
    }
    return res;
}

DFS:

想到BFS我们一般会和DFS联想到一起,DFS是深度优先搜索算法,我们先来看下代码

java

 public int maxDepth(TreeNode root) {
     if (root == null)
         return 0;
     Stack<TreeNode> stack = new Stack<>();
     Stack<Integer> value = new Stack<>();
     stack.push(root);
     value.push(1);
     int max = 0;
     while (!stack.isEmpty()) {
        TreeNode node = stack.pop();
        int temp = value.pop();
        max = Math.max(temp, max);
        if (node.left != null) {
            stack.push(node.left);
            value.push(temp + 1);
        }
        if (node.right != null) {
            stack.push(node.right);
            value.push(temp + 1);
        }
    }
    return max;
}

C++

 public:
 int maxDepth(TreeNode*root) {
     if (root == NULL)
         return 0;
     stack<TreeNode *>nodeStack;
     stack<int> value;
     nodeStack.push(root);
     value.push(1);
     int max = 0;
    while (!nodeStack.empty()) {
        TreeNode * node = nodeStack.top();
        nodeStack.pop();
        int temp = value.top();
        value.pop();
        max = temp > max ? temp : max;
        if (node -> left != NULL) {
            nodeStack.push(node -> left);
            value.push(temp + 1);
        }
        if (node -> right != NULL) {
            nodeStack.push(node -> right);
            value.push(temp + 1);
        }
    }
    return max;
}

这里使用了两个栈,一个是存储节点的,一个是存储每个节点到根节点总共经过多少个节点(包含根节点和当前节点)。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章