Leetcode关于二(N)叉树的高(深)度问题

但看花开落,不言人是非


1. 二叉树的深度

104. 二叉树的最大深度

树的最大深度其实就是树的深度。(PS: 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。)

1.1 DFS解法:

这道题的递归写法就算猜都能猜出来…

 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;
    }
1.2 BFS解法:

求深度,很显然 层次遍历 进行level ++ 肯定可以得到答案

class Solution {
    
    
    public int maxDepth(TreeNode root) {
    
    
        if(root == null) return 0;

       int depth = 0;

       Queue<TreeNode> queue = new LinkedList<>();
       queue.offer(root);
       
       while(!queue.isEmpty()){
    
    
           int size = queue.size();

           while(size -- > 0){
    
    
             TreeNode node =  queue.poll();

             if(node.left != null){
    
    
                 queue.offer(node.left);
             }

             if(node.right != null){
    
    
                 queue.offer(node.right);
             }
           }
           // 每一层进行 ++ 操作
           depth ++;
       }
        return depth;
    }
}

2. 二叉树的最小深度

111. 二叉树的最小深度

趁热打铁,二叉树的最大深度 都搞得定,那最小深度自然不在话下。

2.1 DFS解法:

求最小深度时将Math.max换成Math.min即可,
但要注意如果根节点的左或右子树为空的话是构不成子树的。而最小深度是要求从根节点到子树的

class Solution {
    
    
    public int minDepth(TreeNode root) {
    
    
		if (root == null) {
    
    
			return 0;
		}
		// 只有右子树
        if(root.left == null && root.right != null){
    
    
            return minDepth(root.right)+1;
        }
		// 只有左子树
        if(root.right == null && root.left != null){
    
    
            return minDepth(root.left)+1;
        }

        return Math.min(minDepth(root.left),minDepth(root.right))+1;
		
    }
}
2.2 BFS解法:

一般而言,二叉树的BFS(广度优先遍历) 指的就是:层次遍历

层次遍历中,遇到的第一个叶子结点时的层次,即:最小深度

class Solution {
    
    
    public int minDepth(TreeNode root) {
    
    
		if (root == null) {
    
    
			return 0;
		}

        int depth = 1;

       Queue<TreeNode> queue = new LinkedList<>();
       queue.offer(root);

       while(!queue.isEmpty()){
    
    
           int size = queue.size();
           while(size -- > 0){
    
    
               TreeNode node = queue.poll();
               // 广度优先搜索(层次遍历)的性质保证了最先搜索到的叶子节点的深度一定最小。
               if(node.left == null && node.right == null){
    
    
                   return depth;
               }

               if(node.left != null){
    
    
                   queue.offer(node.left);
               }

               if(node.right != null){
    
    
                   queue.offer(node.right);
               }
           }
           depth ++;
       }
		
        return depth;
    }
}

3. N叉树的深度

559. N 叉树的最大深度

/*
// Definition for a Node.(N叉树的节点定义)
class Node {
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
};
*/

3.1 DFS解法:
class Solution {
    
    
    public int maxDepth(Node root) {
    
    
        if(root == null){
    
    
            return 0;
        }
        int max = 1;
		// N 叉树没有子节点
        if(root.children == null){
    
    
            return 1;
        }
        // 遍历N叉树,求得最大值即可
        for(Node node:root.children){
    
    
            max =  Math.max(max,maxDepth(node)+1);
        }
       return max;
    }
}
3.2 BFS解法:

本质上还是层次遍历

class Solution {
    
    
    public int maxDepth(Node root) {
    
    
       if(root == null){
    
    
           return 0;
       }

       if(root.children == null){
    
    
           return 1;
       }

       Queue<Node> queue = new LinkedList<>();
       queue.offer(root);
        int level = 0;
       while(!queue.isEmpty()){
    
    
           int size = queue.size();

           while(size -- > 0){
    
    
               Node cur = queue.poll();

               if(cur.children != null){
    
    
                   for(Node node:cur.children){
    
    
                       queue.offer(node);
                   }
               }
           }
           level ++;
       }

       return level;
    }
}

4. 总结

凡事涉及到二(N)叉树的深度问题,我们都可以通过层次遍历来搞定它…


5. 练兵场:

剑指 Offer 55 - I. 二叉树的深度

LintCode - 97 · 二叉树的最大深度

LintCode - 155 · 二叉树的最小深度

猜你喜欢

转载自blog.csdn.net/wangcheeng/article/details/117484852