leetcode - Minimum Depth of Binary Tree

题目:Minimum Depth of Binary Tree

Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

 

这道题容易联想到求二叉树最大深度的题目,一开始以为只要把那个题目稍微改改就行了,其实有个小陷阱在里面,在下面的个人思路中我会提及。

 

个人思路:

1、类似求二叉树的深度的算法,采用递归的方式,一个二叉树的最小深度是左子树深度和右子树深度的较小者加1

2、这里要注意加几个判断条件,一个节点为叶子节点,则返回深度1;一个节点若仅有一个左孩子或者右孩子,则应该返回孩子节点的深度加1,而不应该直接计算左右子树的深度,选取较小者加1,假如该节点仅有左孩子,则右节点为空,返回0,这样便会选取右边这条路径,但其实这个右节点根本不存在,就会有问题(读读题目就明白了);一个节点有两个孩子节点,则是按照左右深度较小者加1的思路。

 

代码:

 1 #include <stddef.h>
 2 #include <iostream>
 3 
 4 using namespace std;
 5 
 6 struct TreeNode
 7 {
 8     int val;
 9     TreeNode *left;
10     TreeNode *right;
11     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
12 };
13 
14 class Solution
15 {
16 public:
17     int minDepth(TreeNode *root)
18     {
19         if (!root)
20         {
21             return 0;
22         }
23 
24         if (!root->left && !root->right)
25         {
26             return 1;
27         }
28 
29         if (root->left && !root->right)
30         {
31             return minDepth(root->left) + 1;
32         }
33 
34         if (root->right && !root->left)
35         {
36             return minDepth(root->right) + 1;
37         }
38 
39         int leftMinDepth = minDepth(root->left);
40         int rightMinDepth = minDepth(root->right);
41 
42         return leftMinDepth < rightMinDepth ? leftMinDepth + 1 : rightMinDepth + 1;
43     }
44 };
45 
46 int main()
47 {
48     TreeNode *root = new TreeNode(3);
49     root->left = new TreeNode(9);
50     //root->right = new TreeNode(20);
51     //root->right->left = new TreeNode(15);
52     //root->right->right = new TreeNode(7);
53 
54     Solution s;
55     int depth = s.minDepth(root);
56 
57     cout << depth << endl;
58 
59     system("pause");
60 
61     return 0;
62 }
View Code

 

上面的思路是DFS的方式,在网上还发现了BFS的算法,由于是按层次遍历,只有在某一层发现了叶子节点,则该树的最小深度就是该层次的深度

 

个人实践了一下,代码:

  1 #include <stddef.h>
  2 #include <iostream>
  3 #include <queue>
  4 
  5 using namespace std;
  6 
  7 struct TreeNode
  8 {
  9     int val;
 10     TreeNode *left;
 11     TreeNode *right;
 12     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 13 };
 14 
 15 class Solution
 16 {
 17 public:
 18     int minDepth(TreeNode *root)
 19     {
 20         /*
 21         if (!root)
 22         {
 23             return 0;
 24         }
 25 
 26         if (!root->left && !root->right)
 27         {
 28             return 1;
 29         }
 30 
 31         if (root->left && !root->right)
 32         {
 33             return minDepth(root->left) + 1;
 34         }
 35 
 36         if (root->right && !root->left)
 37         {
 38             return minDepth(root->right) + 1;
 39         }
 40 
 41         int leftMinDepth = minDepth(root->left);
 42         int rightMinDepth = minDepth(root->right);
 43 
 44         return leftMinDepth < rightMinDepth ? leftMinDepth + 1 : rightMinDepth + 1;
 45         */
 46 
 47         if (!root)
 48         {
 49             return 0;
 50         }
 51 
 52         queue<TreeNode *> level, level_bak;
 53         int level_num = 1;
 54         level.push(root);
 55 
 56         while (!level.empty())
 57         {
 58             TreeNode *currentNode = level.front();
 59             level.pop();
 60             level_bak.push(currentNode);
 61             if (!currentNode->left && !currentNode->right)
 62             {
 63                 break;
 64             }
 65             if (level.empty())
 66             {
 67                 ++level_num;
 68                 while (!level_bak.empty())
 69                 {
 70                     currentNode = level_bak.front();
 71                     level_bak.pop();
 72                     if (currentNode->left)
 73                     {
 74                         level.push(currentNode->left);
 75                     }
 76                     if (currentNode->right)
 77                     {
 78                         level.push(currentNode->right);
 79                     }
 80                 }
 81             }
 82         }
 83 
 84         return level_num;
 85     }
 86 };
 87 
 88 int main()
 89 {
 90     TreeNode *root = new TreeNode(3);
 91     root->left = new TreeNode(9);
 92     //root->right = new TreeNode(20);
 93     //root->right->left = new TreeNode(15);
 94     //root->right->right = new TreeNode(7);
 95 
 96     Solution s;
 97     int depth = s.minDepth(root);
 98 
 99     cout << depth << endl;
100 
101     system("pause");
102 
103     return 0;
104 }
View Code

 

posted on 2014-06-20 11:25  laihaiteng  阅读(182)  评论(0编辑  收藏  举报

导航