Minimum Depth of Binary Tree

来源:互联网 发布:淘宝商店如何开通花呗 编辑:程序博客网 时间:2024/06/07 23:27

Given abinary tree, find its minimum depth.

 

The minimumdepth is the number of nodes along the shortest path from the root node down tothe 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:    void recurTree(TreeNode* root,int &depth) {        if(root->left){            root->left->val=root->val+1;            recurTree(root->left,depth);        }        if(root->right){            root->right->val=root->val+1;            recurTree(root->right,depth);        }        if(!root->left&&!root->right){            if(root->val<depth){                depth=root->val;            }        }            }    int minDepth(TreeNode* root) {        if(!root)            return 0;        root->val=1;        int depth=2000;        recurTree(root,depth);        return depth;    }};


解法二

数组实现的层序遍历的非递归算法

/** * 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;        TreeNode* list[2000];        int front=-1,rear=-1;        int level=0,last=0;        list[++rear]=root;        while(front<rear){            TreeNode *temp;            temp=list[++front];            if(temp->left)                list[++rear]=temp->left;            if(temp->right)                list[++rear]=temp->right;            if((!temp->left)&&(!temp->right)){                return level+1;            }            if(front==last){                last=rear;                level++;            }        }        return level;    }};


看了讨论之后,觉得自己的代码不够简洁高效。又重新写了两个改进后的代码


更简洁的dfs解法

/** * 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&&root->right)            return min(minDepth(root->left),minDepth(root->right))+1;        return max(minDepth(root->left),minDepth(root->right))+1;    }};

用queue实现的层序遍历非递归算法

/** * 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;        queue<TreeNode*> q;        int level=0;        q.push(root);        while(!q.empty()){            int vol=q.size();            level++;            for(int i=0;i<vol;i++){                TreeNode *node;                node=q.front();                if(!node->left&&!node->right)                    return level;                if(node->left)                    q.push(node->left);                if(node->right)                    q.push(node->right);                q.pop();            }        }        return level;    }};



0 0
原创粉丝点击