【LeetCode】:二叉树的Max,Min深度

 

一、最大深度问题

 

描述:

 

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.

For example:
Given binary tree [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

return its depth = 3.

 解答:

/**
 * 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;
    }
};

 

 

 

 

二、最小深度问题

 

最小深度是沿着从根节点到最近叶节点的最短路径的节点数量

 

描述:

 

Given a binary tree, find its minimum depth.

 

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

 

解答:

 

/**
 * 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 minDepth(TreeNode* root) {
        if(!root) 
            return 0;
        if(!root->left) 
            return 1 + minDepth(root->right);
        if(!root->right) 
            return 1 + minDepth(root->left);
        return min(minDepth(root->left),minDepth(root->right)) + 1;
    }
};

 

 

 

二、java:

描述:

-------------------------------------------------

给定一个二叉树,找出其最小深度。

二叉树的最小深度为根节点到最近叶子节点的距离。
样例

给出一棵如下的二叉树:

        1

     /     \ 

   2       3

          /    \

        4      5  

这个二叉树的最小深度为 2

-----------------------------------------------------------

给定一个二叉树,找出其最大深度。

二叉树的深度为根节点到最远叶子节点的距离。

样例

给出一棵如下的二叉树:

  1
 / \ 
2   3
   / \
  4   5

这个二叉树的最大深度为3.

------------------------------------------------------------

解题思路:

此类型的题目使用遍历子树的方法,递归找到左子树和右子树,多一层计数加1,最后左子树与右子树的层数比较,选出最小或者最大深度。其中最小深度因为有0干扰,需要多个条件判断。

代码如下:

public class 二叉树的最大深度 {
    /**
     * Definition of TreeNode:
     */
    public class TreeNode {
        public int val;
        public TreeNode left, right;

        public TreeNode(int val) {
            this.val = val;
            this.left = this.right = null;
        }
    }
    
    /**
     * @param root: The root of binary tree.
     * @return: An integer.
     */
    public int maxDepth(TreeNode root) {
        // write your code here
        int res=0;
        res=depth(res,root);
        return res;
    }
    
    public int depth(int res,TreeNode root){
        if(root==null)
            return res;
        if(root.left==null && root.right==null)
            return res+1;
        int res1=depth(res,root.left)+1;
        int res2=depth(res,root.right)+1;
        res = Math.max(res1,res2);
        return res;
    }
}

//*************************************

public class 二叉树的最小深度 {
    
     /**
     * @param root: The root of binary tree.
     * @return: An integer.
     */
    public int minDepth(TreeNode root) {
        // write your code here
        int res=0;
        res=depth(res,root);
        return res;
    }
    
    public int depth(int res,TreeNode root){
        if(root==null)
            return res;
        if(root.left==null && root.right==null)//只有当左右子树都为空时才是叶子节点,这里不能用“||”
            return res+1;
        if(root.left!=null && root.right==null) //分别判断左右子树各自为空情况
            return res=depth(res,root.left)+1;
        if(root.left==null && root.right!=null)
            return res=depth(res,root.right)+1;
        int res1=depth(res,root.left)+1;
        int res2=depth(res,root.right)+1;
        res = Math.min(res1,res2);
        return res;
    }
}

 

posted @ 2017-08-21 11:19  华不摇曳  阅读(402)  评论(0编辑  收藏  举报