`
king_tt
  • 浏览: 2325347 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

【leetcode】Maximum Depth of Binary Tree

 
阅读更多

Question :

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.

Anwser 1 : DFS

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int maxDepth(TreeNode *root) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(root == NULL) return 0;
        
        int l = maxDepth(root->left);
        int r = maxDepth(root->right);
        
        return l > r ? l + 1 : r + 1;
    }
};


More simpe :

class Solution {
public:
    int maxDepth(TreeNode *root) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(root == NULL) return 0;
        return 1 + max( maxDepth(root->left), maxDepth(root->right) );
    }
};


Anwser 2 : BFS in queue

class Solution {
public:
    int maxDepth(TreeNode *root) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(root == NULL) return 0;
        
        queue<TreeNode *> Q;
        
        Q.push(root);
        int count = 1;
        int depth = 0;
        while(!Q.empty()){
            TreeNode *tmp = Q.front();
            Q.pop();
            count--;
            
            if(tmp->left){
                Q.push(tmp->left);
            }
            if(tmp->right){
                Q.push(tmp->right);
            }
            
            if(count == 0){
                depth++;            // one row is end, add a depth
                count = Q.size();   // next row count
            }
        }
        return depth;
    }
};



Anwser 3 : BFS in stack

class Solution {
public:
    int maxDepth(TreeNode *root) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(root == NULL) return 0;
        
        stack<TreeNode*> S;
        
        int maxDepth = 0;
        TreeNode *prev = NULL;
        
        S.push(root);
        while (!S.empty()) {
            TreeNode *curr = S.top();
            
            if (prev == NULL || prev->left == curr || prev->right == curr) {
                if (curr->left)
                    S.push(curr->left);
                else if (curr->right)
                    S.push(curr->right);
            } else if (curr->left == prev) {
                if (curr->right)
                    S.push(curr->right);
            } else {
                S.pop();
            }
            prev = curr;
            if (S.size() > maxDepth)
                maxDepth = S.size();
        }
        return maxDepth;
    }
};


参考推荐:

Maximum Depth of Binary Tree

树与二叉树的深度优先与广度优先算法


分享到:
评论

相关推荐

    java-leetcode-maximum-depth-of-binary-tree

    java java_leetcode-maximum-depth-of-binary-tree

    python-leetcode题解之104-Maximum-Depth-of-Binary-Tree

    《LeetCode题解之104-Maximum Depth of Binary Tree》是一篇针对编程题目的详细解释与Python代码示例的文章。题目要求编写一个函数来找出二叉树的最大深度。在计算机科学中,二叉树的深度是指从根节点到最远叶子节点...

    js-leetcode题解之104-maximum-depth-of-binary-tree.js

    文件标题为“js-leetcode题解之104-maximum-depth-of-binary-tree.js”,这表明文件内容是关于JavaScript语言的LeetCode题解,具体为第104题——求二叉树的最大深度。二叉树是一种常见的数据结构,拥有丰富的应用...

    LeetCode和剑指offer中的算法题的题目和解法 和 常见算法汇总

    1. Math Implementation Questions(数学实现题) ...5.1 Maximum Depth of Binary Tree(二叉树的深度) 5.2 Invert Binary Tree(反转二叉树) 5.3... 5.4... 5.5... 6. String Questions(字符串相关问题)

    Leetcode 使用 Javascript 的解决方案.zip

    Javascript 的解决方案Leetcode Problems and interview problems in Javascript10 Regular Expresion Matching.js100 Same Tree.js101 Symmetric Tree.js102 Binary Tree Level Order Traversal.js103 Binary Tree ...

    Leetcode的ac是什么意思-LeetCodeInJava:leetcode-java

    Leetcode的ac是什么意思 LeetCodeInJava List #98 Validate Binary Search Tree #100 Same Tree #104 Maximum Depth of Binary Tree #122 Best Time to Buy and Sell Stock II #136 Single Number #150 Evaluate ...

    leetcode答案-leetcode-java:leetcode的Java代码

    leetcode 答案leetcode-java leetcode.com 的 Java 答案 ================索引================ com.leetcode.array Search a ...com.leetcode.list ...com.leetcode.string ...Maximum Depth of Binary Tree Same Tree

    javalruleetcode-what_the_dead_men_say:what_the_dead_men_say

    java lru leetcode what_the_dead_men_say 所以这只是一个 repo,我从leetcode.com存储我的问题解决方案。 二叉树 0098 Validate Binary ...Tree ...Binary ...Maximum depth of binary tree - Java Iterative

    Leetcode book刷题必备

    26. Maximum Depth of Binary Tree:计算二叉树的最大深度。 27. Minimum Depth of Binary Tree:计算二叉树的最小深度。 28. Balanced Binary Tree:判断一个二叉树是否是平衡二叉树。 29. Convert Sorted Array to...

    LeetCode去除数组重复元素-Arithmetic-Swift:一些算法的swift实现

    LeetCode去除数组重复元素 Arithmetic-Swift 一些算法的swift实现 桶排序 冒泡排序 快速排序 ##正好看见LeetCode可以刷Swift的题目 开始慢慢刷 swift有playground 做起来还是相当方便的 已完成题目 ----2016.9.30 两...

    leetcode-js:算法和数据结构是一个程序员的灵魂,LeetCode JavaScript TypeScript 题解

    leetcode-js Leecode 经典题目 JavaScript TypeScript 题解。 Leetcode's answers by ...104.二叉树的最大深度 (Maximum Depth of Binary Tree) 118.杨辉三角 (Pascal's Triangle) 119.杨辉三角 II (Pascal's Triangle)

    LeetCode最全代码

    318| [Maximum Product of Word Lengths](https://leetcode.com/problems/maximum-product-of-word-lengths/) | [C++](./C++/maximum-product-of-word-lengths.cpp) [Python](./Python/maximum-product-of-word-...

    Leetcode题目+解析+思路+答案.pdf

    - **Depth of Binary Tree**:计算二叉树的深度。 - **Construct Binary Tree**:根据给定的数组构建二叉树。 - **Binary Tree Level Order Traversal**:二叉树的层次遍历。 - **Symmetric Tree**:判断一个...

    四平方和定理leetcode-leetcode-practice:个人LeetCode练习代码

    104.maximum-depth-of-binary-tree (二叉树的最大深度) 105.construct-binary-tree-from-preorder-and-inorder-traversal (从前序与中序遍历序列构造二叉树) 106.construct-binary-tree-from-inorder-and-postorder-...

    leetcode答案-LeetCode-Trip:LeetCode刷题代码,大佬勿入

    leetcode 答案 LeetCode-Trip LeetCode刷题代码,大佬勿入。...Depth of Binary Tree] [121. Best Time to Buy and Sell Stock] [167. Two Sum II - Input array is sorted] Medium [2. Add Two Numbers]

    leetcode答案-leetcode:leetcode

    Maximum Depth of Binary Tree 这?也太简单了吧。。一行代码,一个尾递归搞定啊。。 终于想清楚了,leetcode的AC率应该是:在线编辑、肉眼检查,提交的准确率!借助线下debug工具,有何难度可言?丝毫没有模拟在线...

    leetcode java

    - 计算二叉树的最大深度(Maximum Depth of Binary Tree)考察递归的使用。 - 平衡二叉树(Balanced Binary Tree)问题需要检查一棵树是否是平衡的。 **位操作(Bit Manipulation)** 位操作是高级的编程技能,涉及...

    LeetCode,《剑指offer》中的算法题的题目和解法以及常见算法的实现

    Maximum Depth of Binary Tree"要求计算一棵二叉树的最大深度。 4. 动态规划:动态规划是一种将复杂问题分解为子问题,通过状态转移方程求解的方法。比如,"121. Best Time to Buy and Sell Stock"股票买卖问题,...

    常见算法题答案及解析

    26. Maximum Depth of Binary Tree:二叉树的最大深度。 27. Minimum Depth of Binary Tree:二叉树的最小深度。 28. Balanced Binary Tree:判断二叉树是否为平衡二叉树。 29. Convert Sorted Array to Binary ...

Global site tag (gtag.js) - Google Analytics