++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

给定一个二叉树,找出他的最小的深度。

最小的深度,指的是从根节点到叶子节点的,经历的最小的节点个数。

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

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.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
test.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
 
#include <iostream>
#include <cstdio>
#include <stack>
#include <vector>
#include <queue>
#include "BinaryTree.h"

using namespace std;

int minDepth(TreeNode *root)
{

if(root == NULL)
    {
        return 0;
    }
    queue<TreeNode *> que;
    que.push(root);
    int count = 1;
    int depth = 0;
    while(!que.empty())
    {
        TreeNode *tmp = que.front();
        que.pop();
        count--;

if(tmp->left == NULL && tmp->right == NULL)
        {
            break;
        }

if(tmp->left != NULL)
        {
            que.push(tmp->left);
        }
        if(tmp->right != NULL)
        {
            que.push(tmp->right);
        }
        /*count等于0说明这一层遍历完了,可以depth做加一操作*/
        if(count == 0)
        {
            depth++;
            count = que.size();
        }
    }
    depth++;
    return depth;
}

// 树中结点含有分叉,
//                  8
//              /       \
//             6         1
//           /   \
//          9     2
//               / \
//              4   7
int main()
{
    TreeNode *pNodeA1 = CreateBinaryTreeNode(8);
    TreeNode *pNodeA2 = CreateBinaryTreeNode(6);
    TreeNode *pNodeA3 = CreateBinaryTreeNode(1);
    TreeNode *pNodeA4 = CreateBinaryTreeNode(9);
    TreeNode *pNodeA5 = CreateBinaryTreeNode(2);
    TreeNode *pNodeA6 = CreateBinaryTreeNode(4);
    TreeNode *pNodeA7 = CreateBinaryTreeNode(7);

ConnectTreeNodes(pNodeA1, pNodeA2, pNodeA3);
    ConnectTreeNodes(pNodeA2, pNodeA4, pNodeA5);
    ConnectTreeNodes(pNodeA5, pNodeA6, pNodeA7);

cout << minDepth(pNodeA1) << endl;

DestroyTree(pNodeA1);
    return 0;
}

结果输出:
2
 
BinaryTree.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
#ifndef _BINARY_TREE_H_
#define _BINARY_TREE_H_

struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

TreeNode *CreateBinaryTreeNode(int value);
void ConnectTreeNodes(TreeNode *pParent,
                      TreeNode *pLeft, TreeNode *pRight);
void PrintTreeNode(TreeNode *pNode);
void PrintTree(TreeNode *pRoot);
void DestroyTree(TreeNode *pRoot);

#endif /*_BINARY_TREE_H_*/

BinaryTree.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
 
#include <iostream>
#include <cstdio>
#include "BinaryTree.h"

using namespace std;

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */

//创建结点
TreeNode *CreateBinaryTreeNode(int value)
{
    TreeNode *pNode = new TreeNode(value);

return pNode;
}

//连接结点
void ConnectTreeNodes(TreeNode *pParent, TreeNode *pLeft, TreeNode *pRight)
{
    if(pParent != NULL)
    {
        pParent->left = pLeft;
        pParent->right = pRight;
    }
}

//打印节点内容以及左右子结点内容
void PrintTreeNode(TreeNode *pNode)
{
    if(pNode != NULL)
    {
        printf("value of this node is: %d\n", pNode->val);

if(pNode->left != NULL)
            printf("value of its left child is: %d.\n", pNode->left->val);
        else
            printf("left child is null.\n");

if(pNode->right != NULL)
            printf("value of its right child is: %d.\n", pNode->right->val);
        else
            printf("right child is null.\n");
    }
    else
    {
        printf("this node is null.\n");
    }

printf("\n");
}

//前序遍历递归方法打印结点内容
void PrintTree(TreeNode *pRoot)
{
    PrintTreeNode(pRoot);

if(pRoot != NULL)
    {
        if(pRoot->left != NULL)
            PrintTree(pRoot->left);

if(pRoot->right != NULL)
            PrintTree(pRoot->right);
    }
}

void DestroyTree(TreeNode *pRoot)
{
    if(pRoot != NULL)
    {
        TreeNode *pLeft = pRoot->left;
        TreeNode *pRight = pRoot->right;

delete pRoot;
        pRoot = NULL;

DestroyTree(pLeft);
        DestroyTree(pRight);
    }
}




 
 
 

 

【二叉树的递归】01二叉树的最小深度【Minimum Depth of Binary Tree】的更多相关文章

  1. [LeetCode 111] - 二叉树的最小深度 (Minimum Depth of Binary Tree)

    问题 给出一棵二叉树,找出它的最小深度. 最小深度是指从根节点沿着最短路径下降到最近的叶子节点所经过的节点数. 初始思路 不难看出又是一个需要层次遍历二叉树的题目,只要在112基础上作出简单修改即可得 ...

  2. [Swift]LeetCode111. 二叉树的最小深度 | Minimum Depth of Binary Tree

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

  3. [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

    [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...

  4. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  5. LeetCode OJ Minimum Depth of Binary Tree 递归求解

        题目URL:https://leetcode.com/problems/minimum-depth-of-binary-tree/ 111. Minimum Depth of Binary T ...

  6. [LeetCode] Minimum Depth of Binary Tree 二叉树的最小深度

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

  7. [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度

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

  8. (二叉树 BFS DFS) leetcode 111. Minimum Depth of Binary Tree

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

  9. Minimum Depth of Binary Tree,求树的最小深度

    算法分析:递归和非递归两种方法. public class MinimumDepthofBinaryTree { //递归,树的最小深度,就是它左右子树的最小深度的最小值+1 public int m ...

  10. Minimum Depth of Binary Tree最短深度

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

随机推荐

  1. shell遍历文件目录,监听文件变化,拼接字符串

    最近利用业余时间学习了shell 并做了个例子 实现的功能是 : 监听demo文件夹下的文件,只要新增了  .js的文件就把对应的文件名重组,拼接, 最后写入到demo.js里面. 文件结构如下 : ...

  2. 使用 SourceTree 遇到冲突的解决方法

    首先,更新代码之前先 git stash ,然后 git pull ,再 git stash pop 这时候如果本地改的代码跟线上的冲突了,就报错了.那么就需要手动解决冲突. 打开存在冲突的文件,会看 ...

  3. 2016 acm香港网络赛 C题. Classrooms(贪心)

    原题网址:https://open.kattis.com/problems/classrooms Classrooms The new semester is about to begin, and ...

  4. ios 动画 创建一个UIImageView并将其属性设置animationImages为UIImages 的数组

    NSArray *animationFrames = [NSArray arrayWithObjects: [UIImage imageWithName:@"image1.png" ...

  5. iOS 7 修改默认布局从status bar 底部开始

    最近在对公司的一个老项目进行版本升级,添加了导航栏和tabBar,并且在个人中心界面隐藏navigationBar,于是在控制器里添加了如下对象方法: - (void)viewWillAppear:( ...

  6. 【BZOJ3672】[Noi2014]购票 树分治+斜率优化

    [BZOJ3672][Noi2014]购票 Description  今年夏天,NOI在SZ市迎来了她30周岁的生日.来自全国 n 个城市的OIer们都会从各地出发,到SZ市参加这次盛会.       ...

  7. jquery 访问后台方法 并且获取后方法返回的数据

    说明: 1.开发环境 asp.net MVC4 c#语言. 后台方法位于控制器中ProController.cs中 后台方法如下: public string GetNumber() { string ...

  8. 问题:今天测试模块一直出现一个问题?module 'subprocess' has no attribute 'Popen'

    原因:我起的名字用了模块本身的名字,这个地方一定要切记

  9. Generally a good method to avoid this is to randomly shuffle the data prior to each epoch of training.

    http://ufldl.stanford.edu/tutorial/supervised/OptimizationStochasticGradientDescent/

  10. java访问微信接口发送消息

    最近在开发activiti流程的时候有个需求:流程到达每个审批节点后,需要向该节点的审批人发送一个消息,提示有审批需要处理. 参考了一下微信的开发者文档和网络上的一些技术博客,现在记录一下.以便后续继 ...