【二叉树专题】剑指 Offer 55 – I. 二叉树的深度

本问题对应课程链接:剑指offer刷题第三期

要求:
1、请打卡你的解题代码,如果能简单说一下思路则简单说一下思路。
2、给出你代码所用的时间复杂度和空间复杂度

共以下 54 个回答

  • 大大 普通 2023年11月13日 下午5:14
    c++
    递归计算深度
    int calculateDepth(struct TreeNode* root) {
        if(root==NULL)return 0;
        int l=calculateDepth(root->left);
        int r=calculateDepth(root->right);
        return l>r?(l+1):(r+1);
    }
    
    
    
    
     //层序遍历计算深度
     //tmp用来存储每一层
    
     //q记录的是每层的节点,不过没有分开 
     //现在引入tmp数组,将每一层分开
    class Solution {
    public:
         queue<TreeNode*>q,tmp;
         int res=0;
        int calculateDepth(TreeNode* root) {
          if(root==NULL)return 0;
          q.push(root);
          res++;
           while(q.empty()!=true){
            while(q.empty()!=true){
              TreeNode* head=q.front();
              q.pop();
              if(head->left)
              tmp.push(head->left);
              if(head->right)
              tmp.push(head->right);
              }
            if(tmp.empty()!=true)res++;
            q=tmp;
            queue<TreeNode*>empty;
            swap(tmp,empty);
           }
            return res;
           }    
    };
    
    0 赞同 0 条回复
  • 九号球 普通 2023年11月11日 上午11:33

    “`java
    两种解法,递归和层次遍历
    class Solution {
    public int calculateDepth(TreeNode root) {
    if (root == null) {
    return 0;
    }
    Queue queue = new LinkedList<>();
    int res = 0;
    queue.add(root);
    while(!queue.isEmpty()) {
    int n = queue.size();
    for (int i = 0; i < n; i++) { TreeNode node = queue.poll(); if (node.left != null) { queue.add(node.left); } if (node.right != null) { queue.add(node.right); } } res++; } return res; } }

    0 赞同 0 条回复
  • 招舟子 普通 2023年8月25日 下午3:54

    “`java
    /**
    * Definition for a binary tree node.
    * public class TreeNode {
    * int val;
    * TreeNode left;
    * TreeNode right;
    * TreeNode(int x) { val = x; }
    * }
    */
    class Solution {
    public int maxDepth(TreeNode root) {
    if(root==null){
    return 0;
    }
    int left = maxDepth(root.left);
    int right = maxDepth(root.right);
    return Math.max(left,right)+1;

    }
    

    }
    递归三部曲:先明确函数作用,再确定递归结束条件,最后函数关系式

    0 赞同 0 条回复
  • 淡色 花樱 普通 2023年7月18日 下午5:51

    “`java
    思路 递归
    时间复杂度 0(n) 空间复杂度0(1)
    class Solution{
    public int maxDepth(TreeNode root){
    if(root == null) return 0;
    int left =maxDepth(root.left);
    int right = maxDepth(root.right);
    return Math.max(left,right)+1;
    }

    0 赞同 0 条回复
  • cute. 普通 2023年7月10日 上午11:27

    思路:递归

    var maxDepth = function(root) {
        if(root === null) return 0;
        let left = maxDepth(root.left);
        let right = maxDepth(root.right);
        return Math.max(left, right) + 1;
    };
    
    0 赞同 0 条回复
  • BOZ 普通 2023年7月6日 下午10:00

    【思路】简单递归
    【复杂度】时间O(n),空间O(1)

    class Solution {
    public:
        int maxDepth(TreeNode* root) {
            if (root == NULL) return 0;
            if (root->left == NULL && root->right == NULL) return 1;
            return std::max(maxDepth(root->left), maxDepth(root->right)) + 1;
        }
    };
    
    0 赞同 0 条回复
  • mpweixin用户 普通 2023年7月6日 下午1:02

    C++递归实现

    class Solution {
    public:
        int maxDepth(TreeNode* root) {
            int depth = 0;
            if(root == nullptr){
                return 0;
            }
            if(root -> left == nullptr && root -> right == nullptr){
                return 1;
            } 
            depth =  max(maxDepth(root -> left), maxDepth(root -> right)) + 1;
            return depth;
        }
    };
    
    0 赞同 0 条回复
  • 憨憨 永久会员 2023年7月5日 下午6:01

    递归实现

    class Solution {
        public int maxDepth(TreeNode root) {
            if(root==null)return 0;
            return Math.max(maxDepth(root.left),maxDepth(root.right))+1;
    
    
        }
    }
    
    0 赞同 0 条回复
  • Sexypig. 普通 2023年7月4日 下午2:35

    这段代码定义了一个TreeNode类和一个Solution类。TreeNode类表示二叉树的节点,包含一个值属性和左右子节点属性。Solution类包含一个方法maxDepth,用于计算二叉树的最大深度。
    maxDepth方法使用递归的方式计算二叉树的最大深度。首先判断根节点是否为空,如果为空则返回0。如果根节点不为空,则递归调用maxDepth方法计算左子树和右子树的最大深度,并取其中较大的值。最后将较大值加1,表示当前节点的深度,作为结果返回。
    这段代码的时间复杂度为O(n),其中n为二叉树的节点数。因为每个节点只会被访问一次。空间复杂度为O(h),其中h为二叉树的高度,因为递归调用会使用到函数调用栈。

    # Definition for a binary tree node.
    class TreeNode:
        def __init__(self, x):
            self.val = x
            self.left = None
            self.right = None
    
    class Solution:
        def maxDepth(self, root: TreeNode) -> int:
            if not root:
                return 0
            return max(self.maxDepth(root.left),self.maxDepth(root.right))+1
    
    if __name__ == '__main__':
        s=Solution()
        root=TreeNode(1)
        root.left=TreeNode(2)
        root.right=TreeNode(3)
        root.left.left=TreeNode(4)
        print(s.maxDepth(root))
    
    0 赞同 0 条回复
  • 名字什么的好麻烦 普通 2023年7月4日 上午11:39

    队列广搜

    class Solution {
        public int maxDepth(TreeNode root) {
            if(root==null)
                return 0;
            Queue<TreeNode> queue = new LinkedList<>();
            queue.offer(root);
            int depth=0;
            while(!queue.isEmpty()){
                int size = queue.size();
                for(int i=0;i<size;i++){
                    TreeNode node = queue.poll();
                    if(node.left!=null)
                        queue.offer(node.left);
                    if(node.right!=null)
                        queue.offer(node.right);
                }
                depth++;
            }
            return depth;
        }
    }
    
    0 赞同 0 条回复
  • 橙子陈 普通 2023年7月3日 下午11:45
    class Solution {
        public int maxDepth(TreeNode root) {
            return f(root);
        }
    
        int f(TreeNode t){
            if (null == t) {
              return 0;
            }
    
            return 1 + Math.max(f(t.left), f(t.right));
        }
    }
    

    时间复杂度O(n)
    空间复杂度O(n)

    0 赞同 0 条回复
  • 小金子 普通 2023年7月3日 下午10:41

    /**时间复杂度 O(n),空间复杂度O(h)
    * Definition for a binary tree node.
    * type TreeNode struct {
    * Val int
    * Left *TreeNode
    * Right *TreeNode
    * }
    */
    func maxDepth(root *TreeNode) int {
    if rootnil{
    return 0
    }
    l:=maxDepth(root.Left)
    r:=maxDepth(root.Right)
    if l>=r{
    return l+1
    }else{
    return r+1
    }

    }

    0 赞同 0 条回复
  • GGBond 普通 2023年7月3日 下午5:29
    //时间复杂度:每个结点遍历一遍O(n),空间复杂度: 栈保存结点 O(n)
    class Solution {
        public int maxDepth(TreeNode root) {
            return check(root);
        }
    
        public int check(TreeNode node){
            if(node == null){
                return 0;
            }
            return Math.max(check(node.left)+1,check(node.right)+1);
        }
    }
    
    0 赞同 0 条回复
  • mpweixin用户 普通 2023年7月2日 下午1:59
    class Solution {
        public int maxDepth(TreeNode root) {
            if(root == null) {
                return 0;
            }
            int left = maxDepth(root.left);
            int right = maxDepth(root.right);
            return (left > right ? left : right) + 1;
    
        }
    }
    

    时间复杂度o(h), 空间复杂度o(H)

    0 赞同 0 条回复
  • mpweixin用户 普通 2023年7月2日 下午12:15
    递归
    # Definition for a binary tree node.
    # class TreeNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution:
        def maxDepth(self, root: TreeNode) -> int:
            if not root:
                return 0
            return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1
    
    0 赞同 0 条回复
  • CV大神 普通 2023年6月30日 下午11:17
    public int maxDepth(TreeNode root) {
            //递归终止条件
            if (root == null) {
                return 0;
            }
            //左边或者右边深度最大值+1,则为当前树最大深度
            return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
        }
    

    时间复杂度O(n),空间复杂度O(n)

    0 赞同 0 条回复
  • 花老爷 普通 2023年6月29日 下午11:37

    dfs
    时间:O(n)
    空间:O(height)

    class Solution {
        private int max;
    
        public int maxDepth(TreeNode root) {
            reserve(0, root);
            return max;
        }
    
        private void reserve(int depth, TreeNode node) {
            if (node == null) {
                return;
            }
            depth++;
            max = Math.max(max, depth);
            reserve(depth, node.left);
            reserve(depth, node.right);
        }
    }
    

    bfs
    时间:O(n)
    空间:O(n)

    class Solution {
        public int maxDepth(TreeNode root) {
            if (root == null) {
                return 0;
            }
            int depth = 0;
            Deque<TreeNode> queue = new ArrayDeque<>();
            queue.offer(root);
            while (!queue.isEmpty()) {
                int size = queue.size();
                for (int i = 0; i < size; i++) {
                    TreeNode node = queue.poll();
                    if (node.left != null) {
                        queue.offer(node.left);
                    }
                    if (node.right != null) {
                        queue.offer(node.right);
                    }
                }
                depth++;
            }
            return depth;
        }
    }
    
    0 赞同 0 条回复
  • 拾陆. 普通 2023年6月29日 下午10:16
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        int maxDepth(TreeNode* root) {
            if(root == nullptr){
                return 0;
            }
            int left_depth = maxDepth(root->left);
            int right_depth = maxDepth(root->right);
            return max(left_depth, right_depth) + 1;
        }
    };
    

    可以

    0 赞同 0 条回复
  • mpweixin用户 普通 2023年6月29日 上午6:07

    时间复杂度O(n)
    空间复杂度O(n)

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
        int depth = 0;
    public:
        int maxDepth(TreeNode* root) {
            findDepth(root, 1);
            return depth;
        }
        void findDepth(TreeNode* curr, int currDep) {
            if (!curr) {
                return;
            }
            depth = max(currDep, depth);
            findDepth(curr->left, currDep + 1);
            findDepth(curr->right, currDep + 1);
        }
    };
    
    0 赞同 0 条回复
  • 别恰冰水🧊 永久会员 2023年6月28日 下午3:21

    时间复杂度O(n),空间复杂度O(n)

    int maxDepth(struct TreeNode* root){
        if(root == NULL){
            return 0;
        }
        int left = maxDepth(root->left);
        int right = maxDepth(root->right);
    
        return fmax(left,right) + 1;
    }
    
    0 赞同 0 条回复
  • 局外人 普通 2023年6月28日 上午12:16

    思路:递归
    时间复杂度:O(n), 空间复杂度:O(n)

    class Solution {
        public int maxDepth(TreeNode root) {
            if(root==null)  return 0;
            return Math.max(maxDepth(root.left), maxDepth(root.right))+1;
        }
    }
    
    0 赞同 0 条回复
  • 张君雅小妹妹 普通 2023年6月27日 下午9:36

    时间复杂度:O(N)
    空间复杂度:O(logN)

    class Solution {
        // 1. 明确函数功能
        public int maxDepth(TreeNode root) {
            // 2. 递归结束的条件
            if(root == null) return 0;
            // 3. 找出函数的等价关系式
            int leftLen = maxDepth(root.left) + 1;
            int rightLen = maxDepth(root.right) + 1;
            return Math.max(leftLen, rightLen);
        }
    }
    
    0 赞同 0 条回复
  • 地平线真勇敢 普通 2023年6月27日 下午8:04

    public int maxDepth(TreeNode root){
    if(root null) return 0;
    int left = maxDepth(root.left);
    int right = maxDepth(root.right);

    return Math.max(left,right) + 1;
    

    }
    时间复杂度o(n)
    空间复杂度o(n)

    0 赞同 0 条回复
  • 小吕 普通 2023年6月27日 下午3:12

    c++递归
    int getMaxdeep(TreeNode* temp)
    {
    if(tempNULL) return 0;
    int leftdepth = getMaxdeep(temp->left);
    int rightdepth = getMaxdeep(temp->right);
    return 1+max(leftdepth,rightdepth);
    }
    int maxDepth(TreeNode* root) {
    int deep = getMaxdeep(root);
    return deep;
    }

    0 赞同 0 条回复
  • ~ 普通 2023年6月26日 下午10:32

    class Solution {
    int depth = 0;
    int res = 0;
    public:
    int maxDepth(TreeNode* root) {
    traverse(root);
    return res;
    }
    void traverse(TreeNode* root){
    if(rootnullptr){
    return;
    }
    depth++;
    if(root->left
    nullptr&&root->rightnullptr){
    res = max(res, depth);
    }
    traverse(root->left);
    traverse(root->right);
    depth–;
    }
    };
    时间复杂度O(N),空间复杂度O(1)

    0 赞同 0 条回复
  •  普通 2023年6月26日 下午4:07
    class Solution {
        public int maxDepth(TreeNode root) {
            if(root == null){
                return 0;
            }
            return Math.max(maxDepth(root.left),maxDepth(root.right)) + 1;
        }
    }
    

    后序遍历DFS,时间复杂度 O(n)

    0 赞同 0 条回复
  • mpweixin用户 普通 2023年6月26日 下午2:50
    int maxDepth(struct TreeNode* root){
    if(root==NULL) return 0;
    return fmax(maxDepth(root->left),maxDepth(root->right))+1;
    }
    

    时间复杂度为O(N)
    空间复杂度为O(H),其中H是二叉树的高度

    0 赞同 0 条回复
  • Bello~ 普通 2023年6月26日 上午11:08

    时间复杂度O(N)
    空间复杂度O(树高)

    class Solution {
        public int maxDepth(TreeNode root) {
            if (root == null) {
                return 0;
            }
            int deep = 1;
            return deep  += Math.max(maxDepth(root.left), maxDepth(root.right));
        }
    }
    
    0 赞同 0 条回复
  • 残月冷骄阳 普通 2023年6月26日 上午9:52

    复杂度

    • 时间复杂度: $O(n)$

    • 空间复杂度: $O(height)$

    Code

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public int maxDepth(TreeNode root) {
            if(root==null)return 0;
            return Math.max(maxDepth(root.left),maxDepth(root.right))+1;
        }
    }
    
    0 赞同 0 条回复
  • 哈哈哈 普通 2023年6月25日 下午11:58
    • 思路:使用最传统的递归方法,退出条件:当节点为空时,return 0 ;不为空时,左节点、右节点分别进行递归,返回最大的深度数。
    • 时间复杂度为O(n),空间复杂度为树的深度
    • 但我这里有个问题就是,当我将m、n设置为全局变量的时候,输入案例为[1,2,3,4,5]时,输出结果应该为3,但我的结果为2???
    class Solution {
        public int maxDepth(TreeNode root) {
            if(root==null)
            return 0;
            int m=maxDepth(root.left);
            int n=maxDepth(root.right);
            return m > n ? m+1 : n+1;
        }
    }
    
    0 赞同 0 条回复
  • 彦,,# 普通 2023年6月25日 下午11:49

    递归时间复杂度O(n)空间复杂度为递归的深度

    int maxDepth(TreeNode* root) {
            if(!root) return 0;
            if(!root->left && !root->right) return 1;
            return  max(maxDepth(root->left), maxDepth(root->right))+1;
        }
    
    0 赞同 0 条回复
  • mpweixin用户 普通 2023年6月25日 下午8:55

    ”’
    思路:递归
    C++
    class Solution {
    public:
    int maxDepth(TreeNode* root) {
    if (root nullptr) return 0;
    return max(maxDepth(root->left), maxDepth(root->right)) + 1;
    }
    };
    时间复杂度:二叉树中有n个节点,遍历整个二叉树,时间复杂度O(n);
    空间复杂度:O(height),其中height 表示二叉树的高度。递归函数需要栈空间,而栈空间取决于递归的深度,因此空间复杂度等价于二叉树的高度。
    ”’

    0 赞同 0 条回复
  • Dsjh 普通 2023年6月25日 下午8:02
    // c++实现,递归
    class Solution {
    public:
        int maxDepth(TreeNode* root) {
            if(!root){
                return 0;
            }
            return max(1+maxDepth(root->left),1+maxDepth(root->right));
        }
    };
    
    0 赞同 0 条回复
  • 屠龙手段 普通 2023年6月25日 下午7:43
    递归
    # Definition for a binary tree node.
    # class TreeNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution:
        def maxDepth(self, root: TreeNode) -> int:
            if not root:
                return 0
            return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1
    
    0 赞同 0 条回复
  • 伏月三十 普通 2023年6月25日 下午7:36

    递归:时间复杂度O(n),空间复杂度O(n)

    class Solution {
        public int maxDepth(TreeNode root) {
            if(root==null){
                return 0;
            }
            int left=maxDepth(root.left);
            int right=maxDepth(root.right);
            return Math.max(left,right)+1;
        }
    }
    
    0 赞同 0 条回复
  • 硬核洋葱 普通 2023年6月25日 下午6:09
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        int maxDepth(TreeNode* root) {
            if(!root)   return 0;
            return max(maxDepth(root->left), maxDepth(root->right)) + 1;        
        }
    };
    

    递归,以 NULL 为递归终点
    时间复杂度O(n) 空间复杂度O(h)

    0 赞同 0 条回复
  • 张开怀 普通 2023年6月25日 下午2:15
    public:
          int  maxDepth(TreeNode* root){
                if(!root){
                      return 0;
                }
                int left = maxDepth(root->left);
                int right = maxDepth(root->right);
    
                return max(left,right) + 1;
          }
    

    时间复杂度:O(n)
    空间复杂度:O(1)

    0 赞同 0 条回复
  • désire. 普通 2023年6月25日 上午9:40

    依照层序遍历:
    class Solution {
    public:
    int maxDepth(TreeNode* root) {
    if(root NULL) return 0;
    queue<TreeNode> que;
    que.push(root);
    int depth = 0;
    while(!que.empty())
    {
    int size = que.size();
    depth++;
    for(int i = 0;i < size;i++)
    {
    TreeNode
    node = que.front();
    que.pop();
    if(node->left) que.push(node->left);
    if(node->right) que.push(node->right);
    }

        }
        return depth;
    }
    

    };

    0 赞同 0 条回复
  • Vow the vow。 普通 2023年6月25日 上午8:28

    时间复杂度:O(n)
    空间复杂度:O(1)

    public int maxDepth(TreeNode node) {
            if(node == null) return 0;
            int leftDepth = maxDepth(node.left);
            int rightDepth = maxDepth(node.right);
            return Math.max(leftDepth,rightDepth) + 1;
        }
    
    0 赞同 0 条回复
  • 哈哈哈哈开开心心 普通 2023年6月25日 上午1:50
    # Definition for a binary tree node.
    # class TreeNode:
    #     def __init__(self, val=0, left=None, right=None):
    #         self.val = val
    #         self.left = left
    #         self.right = right
    class Solution:
        def maxDepth(self, root: Optional[TreeNode]) -> int:
            if not root:
                return 0
    
            return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))
    

    时间复杂度:O(n)

    0 赞同 0 条回复
  • 大鑫啊 普通 2023年6月25日 上午1:15

    时间复杂度:O(n)
    空间复杂度:O(n)
    1、递归思路
    2、确定方法结构,确定返回值,确定缩小的递归的等式关系

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public int maxDepth(TreeNode root) {
            if(root == null)return 0;
            int left = maxDepth(root.left);
            int right = maxDepth(root.right);
            return Math.max(left,right)+1;
        }
    }
    
    0 赞同 0 条回复
  • 怀瑾 永久会员 2023年6月24日 下午11:05

    时间复杂度:O(n)
    空间复杂度:O(n)
    思路:首先判断该节点是否为NULL,如果是,返回0递归结束条件
    递归左子树,递归右子树,返回左右路径的最大值+1。

    class Solution {
    public:
        int maxDepth(TreeNode* root) {
            if(root == NULL) return 0;
            int left = maxDepth(root->left);
            int right = maxDepth(root->right);
            return max(left, right)+1;
        }
    };
    
    0 赞同 0 条回复
  • 江畅 普通 2023年6月24日 下午10:20
    // 时间复杂度 O(N)
    // 空间复杂度 O(Height)
    class Solution {
    public:
        int maxDepth(TreeNode* root) {
            if (root == nullptr) {
                return 0;
            }
    
            return max(maxDepth(root->left), maxDepth(root->right)) + 1;
        }
    };
    
    0 赞同 0 条回复
  • Ming 普通 2023年6月24日 下午10:20

    时间复杂度O(n);
    空间复杂度O(height);

    public int maxDepth(TreeNode root) {
            if(root == null) return 0;
            int left = maxDepth(root.left);
            int right = maxDepth(root.right);
            return Math.max(left, right) + 1;
        }
    
    0 赞同 0 条回复
  • Jerry 普通 2023年6月24日 下午8:59
    //回溯法
    class Solution1 {
        int depth;
        int maxDepth; // 记录最大深度
        public int maxDepth(TreeNode root) {
            traverse(root);
            return maxDepth;
        }
    
        void traverse(TreeNode root) {
            if (root == null) return;
            depth++;
            maxDepth = Math.max(maxDepth, depth);
            traverse(root.left);
            traverse(root.right);
            depth--;
        }
    }
    

    时间复杂度:O(n)
    空间复杂度:O(1)


    //动态规划法
    class Solution2 {
        public int maxDepth(TreeNode root) {
            if (root == null) return 0;
            int leftMaxDepth = maxDepth(root.left);
            int rightMaxDepth = maxDepth(root.right);
            return Math.max(leftMaxDepth, rightMaxDepth) + 1;
        }
    }
    

    时间复杂度:O(n)
    空间复杂度:O(1)

    0 赞同 0 条回复
  • yxy 普通 2023年6月24日 下午7:37

    时间复杂度O(n),空间复杂度O(n)

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        int maxDepth(TreeNode* root) {
            if(!root) return 0;
            return max(maxDepth(root->left), maxDepth(root->right))+1;
        }
    };
    
    0 赞同 0 条回复
  • Yodl. 普通 2023年6月24日 下午5:36

    时间复杂度O(nlogn),空间复杂度O(n)

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        int dep(TreeNode* root)
        {
            int l_dep,r_dep;
            if(root==NULL)
            {
                return 0;
            }
            l_dep=dep(root->left);
            r_dep=dep(root->right);
            return (l_dep>r_dep)?(l_dep+1):(r_dep+1);
        }
    
    
        int maxDepth(TreeNode* root) {
            int ans;
            ans=dep(root);
            return ans;
        }
    };
    
    0 赞同 0 条回复
  • mpweixin用户 永久会员 2023年6月24日 下午4:32
    //时间O(n),空间O(n)
    public int maxDepth(TreeNode root) {
            if(root == null){
                return 0;
            }
    
            int left = maxDepth(root.left);
            int right = maxDepth(root.right);
    
            return left > right ? left + 1 : right + 1;
        }
    
    0 赞同 0 条回复
  • peach 普通 2023年6月24日 下午3:46

    思路

    • 递归最简单
    • 使用层序遍历也可以,其实就是看看有多少层
    • 时间复杂度O(n) 空间复杂度O(n)

    代码

     //递归思路
    class Solution {
        public int maxDepth(TreeNode root) { //这个方法的作用是找到一个二叉树的深度,传入root
            if(root == null) return 0; //终止条件
            return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;//向左向右最大深度再+1
        }
    }
    
     //层序遍历,使用正常层序遍历逻辑,每遍历一层,res++,最后返回res即可
    class Solution {
        public int maxDepth(TreeNode root) {
            if(root == null) return 0;
            Deque<TreeNode> quene = new ArrayDeque();
            quene.addLast(root);
            int size;
            int res = 0;
            while(!quene.isEmpty()){
                size = quene.size();
                while(size-- > 0){
                    root = quene.removeFirst();
                    if(root.left != null) quene.addLast(root.left);
                    if(root.right != null) quene.addLast(root.right);
                }
                res++;
            }
            return res;
        }
    }
    
    0 赞同 0 条回复
  • 雨落圆荷 普通 2023年6月24日 下午3:35

    class Solution {
    //递归
    public int maxDepth(TreeNode root) {
    if(root null) return 0;
    int left = maxDepth(root.left);
    int right = maxDepth(root.right);

        return Math.max(left, right) + 1;
    }
    

    }

    0 赞同 0 条回复
  •          普通 2023年6月24日 下午3:31
    class Solution {
    public:
        int dfs(TreeNode* root){
            if(!root)return 0;
            return max(dfs(root->left),dfs(root->right))+1;
        }
        int maxDepth(TreeNode* root) {
            return dfs(root);
        }
    };
    

    【时间O(logn),空间O(logn)】
    递归计算每个节点

    0 赞同 0 条回复
  • 断舍离 永久会员 2023年6月24日 下午3:08
    class Solution {
        //时间复杂度O(n)
        public int maxDepth(TreeNode root) {
            if(root == null){
                return 0;
            }
            int left = maxDepth(root.left);
            int right = maxDepth(root.right);
            return Math.max(left,right)+1;
        }
    }
    
    0 赞同 0 条回复
  • jhfuture 普通 2023年6月24日 下午3:06
    public int maxDepth(TreeNode root) {
    // 1.要求每一个结点的深度
    //2.停止条件 root==null
    //3.条件表达式 max(left,right)+!
    
        if (root==null) return 0;
        int left=maxDepth(root.left);
        int right=maxDepth(root.right);
       return Math.max(left,right)+1;
        }
    

    时间复杂度为n,空间复杂度为1
    使用递归的方法,重要的是要理解每一个递归的功能,停止条件。

    0 赞同 0 条回复
  • 行动派小彩笔 普通 2023年6月24日 下午2:19
    // 后续遍历
    空间(n),时间O(n) 最坏情况是二叉树退化为链表
    class Solution {
    public:
        int maxDepth(TreeNode* root) {
            if(!root) return 0;
            int left = maxDepth(root->left);
            int right = maxDepth(root->right);
            return max(left,right)+1;
        }
    };
    // 层次遍历
    时间O(n),空间O(n)
    class Solution {
    public:
        int maxDepth(TreeNode* root) {
            if(!root) return 0;
           queue<TreeNode*> Q;
           Q.push(root);
           int ans = 0;
           while(!Q.empty())
           {
               int n =Q.size();
               for(int i = 0;i<n;i++)
               {
                   TreeNode* q = Q.front();
                    Q.pop();
                    if(q->left) Q.push(q->left);
                    if(q->right) Q.push(q->right);
               }
               ans++;
           }
           return ans;
        }
    };
    
    0 赞同 0 条回复

# 回答此问题

后才能回答