104. Maximum Depth of Binary Tree

原创 2016年08月29日 00:15:35

递归

class Solution {
public:
    int maxDepth(TreeNode* root) {
        if(root==0)
            return 0;
        return 1+max(maxDepth(root->left), maxDepth(root->right));/*root!=NULL depth>=1*/
    }
};

DFS

class Solution {
public:
    int maxDepth(TreeNode* root) {
        if(root==NULL)
            return 0;
        stack<TreeNode*> nodes; //save traversal nodes
        int result=0;   //save max depth of node
        nodes.push(root);
        root->val=1;
        while(nodes.size()>0)
        {
            TreeNode* top=nodes.top();
            nodes.pop();
            int curDepth=top->val;
            result=result>curDepth?result:curDepth;
            if(top->left!=NULL)
            {
                nodes.push(top->left);
                top->left->val=curDepth+1;
            }
            if(top->right!=NULL)
            {
                nodes.push(top->right);
                top->right->val=curDepth+1;
            }
        }
        return result;
    }
};

BFS

class Solution {
public:
    int maxDepth(TreeNode* root) {
        if(root==NULL)
            return 0;
        queue<TreeNode*> nodes;
        nodes.push(root);
        root->val=1;
        int result=0;
        while(nodes.size())
        {
            TreeNode *fro=nodes.front();
            int curdepth=fro->val;
            nodes.pop();
            result=result>curdepth?result:curdepth;
            if(fro->left)
            {
                nodes.push(fro->left);
                fro->left->val=curdepth+1;
            }
            if(fro->right)
            {
                nodes.push(fro->right);
                fro->right->val=curdepth+1;
            }
        }
        return result;
    }
};
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

No104. Maximum Depth of Binary Tree

一、题目描述 Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along t...

Leetcode-104. Maximum Depth of Binary Tree c语言

来源:104. Maximum Depth of Binary Tree 题目要求: Given a binary tree, find its maximum depth. The max...
  • tzh476
  • tzh476
  • 2016年07月03日 19:06
  • 519

LeetCode104 Maximum Depth of Binary Tree

详细见:leetcode.com/problems/maximum-depth-of-binary-tree Java Solution: github package leetcod...
  • zxwtry
  • zxwtry
  • 2017年04月27日 22:00
  • 218

104/111 Maximum/Minimum Depth of Binary Tree(二叉树求深度)

104 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the...

leetCode 104. Maximum Depth of Binary Tree

题目链接:https://leetcode.com/problems/maximum-depth-of-binary-tree/     题目内容: Given a binary tree, ...

LeetCode笔记:104.Maximum Depth of Binary Tree

计算二叉树的最大深度

[LeetCode]104.Maximum Depth of Binary Tree

【题目】 Maximum Depth of Binary Tree  Total Accepted: 5260 Total Submissions: 11532My Submissions ...

LeetCode#104. Maximum Depth of Binary Tree

104. Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum dep...

【LeetCode】104. Maximum Depth of Binary Tree(DFS|BFS)

原题Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the lon...

Leetcode 104. Maximum Depth of Binary Tree (Easy) (cpp)

Leetcode 104. Maximum Depth of Binary Tree (Easy) (cpp)
  • Niko_Ke
  • Niko_Ke
  • 2016年07月22日 16:50
  • 274
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:104. Maximum Depth of Binary Tree
举报原因:
原因补充:

(最多只允许输入30个字)