C++ Code


  • 0
    J
    class Solution {
    public:
        int maxDepth(TreeNode* root) {
            stack<TreeNode*> tstk;
            stack<int> istk;
            if(root==NULL)return 0;
            tstk.push(root);istk.push(1);int max=0;
            while(!tstk.empty()){
                int itop=istk.top();istk.pop();
                TreeNode* ttop=tstk.top();tstk.pop();
                if(ttop->left!=NULL){
                    tstk.push(ttop->left);
                    istk.push(itop+1);
                }
                if(ttop->right!=NULL){
                    tstk.push(ttop->right);
                    istk.push(itop+1);
                }
                if(ttop->left==NULL&&ttop->right==NULL){
                    if(itop>max)max=itop;
                }
            }
            return max;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.