LeetCode:Minimum Depth of Binary Tree
2015-10-11 18:43
411 查看
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.
My Solution
1. 二叉树顺序遍历,在回溯时判断是否为叶子节点,若是,则判断层数是否更浅。这个和上一篇几乎一样,只是判断层数的地方由取较小者改为了取较大者。
求最深层数时,比较左右两子树的深度,并使用较大的深度。而求最浅层数时,不能简单比较并使用较小的深度。因为当某个节点没有左孩子但有右孩子时,左边的深度是0,一定是较小者。但这个左孩子并不存在,即该节点的左子树为空,不存在叶子节点。所以这个深度信息是无效的。当某个节点只有左孩子而没有右孩子时同理。所以在代码中要简单做一下判断。
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
My Solution
1. 二叉树顺序遍历,在回溯时判断是否为叶子节点,若是,则判断层数是否更浅。这个和上一篇几乎一样,只是判断层数的地方由取较小者改为了取较大者。
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ #define STACK_LEN 4096 void* stack[STACK_LEN] = {0}; int eleNum = 0; void push(void* stack[], void* elem) { if(eleNum < STACK_LEN) { stack[eleNum++] = elem; } } void* pop(void* stack[]) { return eleNum > 0 ? stack[--eleNum] : NULL; } int minDepth(struct TreeNode* root) { if(NULL == root) { return; } struct TreeNode *current = root; struct TreeNode *last = NULL; int depth = 1; int minDepth = INT_MAX; while(current != NULL) { // 向左下降 if(current->left != NULL && ( last == NULL || last->left == current || last->right == current ) && depth < minDepth ) { ++depth; push(stack, current); last = current; current = current->left; } // 向右下降 else if(current->right != NULL && ( last == NULL || last->left == current || last->right == current || last == current->left ) && depth < minDepth ) { ++depth; push(stack, current); last = current; current = current->right; } // 回溯 else { if(current->left == NULL && current->right == NULL && depth < minDepth) { minDepth = depth; } --depth; last = current; current = (struct TreeNode*)pop(stack); } } return minDepth; }2. 二叉树递归遍历。这里的思路和求最深层数基本一致,但具体代码有些不同。
求最深层数时,比较左右两子树的深度,并使用较大的深度。而求最浅层数时,不能简单比较并使用较小的深度。因为当某个节点没有左孩子但有右孩子时,左边的深度是0,一定是较小者。但这个左孩子并不存在,即该节点的左子树为空,不存在叶子节点。所以这个深度信息是无效的。当某个节点只有左孩子而没有右孩子时同理。所以在代码中要简单做一下判断。
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ int minDepth(struct TreeNode* root) { if(!root) { return 0; } if(root->left == NULL) { return minDepth(root->right) + 1; } else if(root->right == NULL) { return minDepth(root->left) + 1; } else { int leftDepth = minDepth(root->left) + 1; int rightDepth = minDepth(root->right) + 1; return leftDepth > rightDepth ? rightDepth : leftDepth; } }
相关文章推荐
- hdu 5501 The Highest Mark
- 装饰模式学习笔记
- 敏捷
- 数据集市层——论为什么随着技术分析的深入,决策数据报表问题越来越多
- mybatis14 动态sql
- 软件测试管理基础知识
- 【Linux高频命令专题(17)】head
- 测试管理
- ListView组件的使用
- delphi 实现可移动异形窗体(附项目文件)
- 第四周项目3--单链表应用(2)
- 143 - ZOJ Monthly, October 2015 - A(zoj3903)
- 《剑指Offer》面试题:寻找最小的K个数
- light1203 - Guarding Bananas【凸包】
- 143 - ZOJ Monthly, October 2015 I Prime Query 线段树
- BackboneJs入门学习[11]—View实践
- SPOJ 题目 375 QTREE - Query on a tree(树链剖分)
- response.setContentType()的作用及参数
- iOS root权限
- 你为什么不分享[转]