leetcode 104 Maximum Depth of Binary Tree二叉树求深度

Maximum Depth of Binary Tree

Total Accepted: 63668 Total Submissions: 141121 My Submissions

Question Solution

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.

我的解决方案:

/**
 * 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(NULL == root)
            return 0;

        int depth_l = maxDepth(root->left);
        int depth_r = maxDepth(root->right);

        return depth_l > depth_r  ? depth_l + 1:depth_r + 1;

    }
};

一行代码的解法:

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

不用递归的解法:Breadth-first-search

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

不用递归的解法2

int maxDepth(TreeNode *root)
{
    if (root == NULL) return 0;
    stack<TreeNode *> gray;
    stack<int> depth;
    int out = 0;

    gray.push(root);
    depth.push(1);
    while (!gray.empty()) {
        TreeNode *tmp = gray.top();
        int num = depth.top();
        gray.pop();
        depth.pop();
        if (tmp->left == NULL && tmp->right == NULL) {
            out = num > out ? num : out;
        }
        else {
            if (tmp->left != NULL) {
                gray.push(tmp->left);
                depth.push(num + 1);
            }
            if (tmp->right != NULL) {
                gray.push(tmp->right);
                depth.push(num + 1);
            }
        }
    }
    return out;
}

python 的解决方案:

# Definition for a  binary tree node
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    # @param root, a tree node
    # @return an integer
    def maxDepth(self, root):

        def maxDepthHelper(root):
            if not root: return 0
            return max(1+maxDepthHelper(root.left), 1+maxDepthHelper(root.right))

        return maxDepthHelper(root)
时间: 2024-09-30 16:11:35

leetcode 104 Maximum Depth of Binary Tree二叉树求深度的相关文章

[Lintcode]97. Maximum Depth of Binary Tree/[Leetcode]104. Maximum Depth of Binary Tree

97. Maximum Depth of Binary Tree/104. Maximum Depth of Binary Tree 本题难度: Easy Topic: Binary Tree Description 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 f

LeetCode 104. Maximum Depth of Binary Tree (二叉树的最大深度)

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. 题目标签:Tree 这道题目给了我们一个二叉树,要我们找到最大深度,就是从root点到最深的那个点之间点的数量.利用post order 来遍历二叉树,对于每一个点,它的两个chi

Leetcode 104. Maximum Depth of Binary Tree(二叉树的最大深度)

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. 分析:求二叉树的最大深度 解法一:很容易想到的便是递归(深度优先搜索) (1)如果根节点是空,则返回0:否则转到(2) (2)  l = 左子树的最大深度; r = 右子树的最大深

leetCode 104.Maximum Depth of Binary Tree(二叉树最大深度) 解题思路和方法

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. 思路:很基础的一个题,DFS即可.代码如下: /** * Definition for a binary tree node. * public class TreeNode {

104 Maximum Depth of Binary Tree 二叉树的最大深度

给定一个二叉树,找出其最大深度.二叉树的深度为根节点到最远叶节点的最长路径上的节点数.案例:给出二叉树 [3,9,20,null,null,15,7],    3   / \  9  20    /  \   15   7返回最大深度为 3 .详见:https://leetcode.com/problems/maximum-depth-of-binary-tree/description/ 方法一:非递归 /** * Definition for a binary tree node. * st

[leetcode] 104. Maximum Depth of Binary Tree

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. 递归遍历 左子树 和 右子树 一刷: public int maxDepth(TreeNode root) { if(root == null){ return 0; } int

[LeetCode] 104. Maximum Depth of Binary Tree Java

题目: 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. 题意及分析:找出一棵树的高度,即最深子节点.使用深度遍历的方法即可,用一个变量记录遍历到当前点的最大高度,然后当前点若有子节点,遍历到子节点,那么该点的高度+1和当前的最大

[LeetCode]113. Maximum Depth of Binary Tree二叉树的最大深度

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. 解法1:很简单的DFS递归. /** * Definition for a binary tree node. * struct TreeNode { * int val; * T

Java for LeetCode 104 Maximum Depth of Binary Tree

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. 解题思路: 递归,JAVA实现如下: public int maxDepth(TreeNode root) { if(root==null) return 0; return Ma