[LeetCode]104.Maximum Depth of Binary Tree
来源:互联网 发布:linux 网络服务器 编辑:程序博客网 时间:2024/06/11 21:06
【题目】
Maximum Depth of Binary Tree
Total Accepted: 5260 Total Submissions: 11532My SubmissionsGiven a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
【代码】
【递归】
时间复杂度为O(n) 空间复杂度为O(logn)
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: int maxDepth(TreeNode *root) { if(root == NULL){ return 0; } int left = maxDepth(root->left); int right = maxDepth(root->right); return 1 + max(left,right); }};
【队列】
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: //二叉树最大深度(层次遍历,遍历一层高度加1) int maxDepth(TreeNode *root) { int height = 0,rowCount = 1; if(root == NULL){ return 0; } //创建队列 queue<TreeNode*> queue; //添加根节点 queue.push(root); //层次遍历 while(!queue.empty()){ //队列头元素 TreeNode *node = queue.front(); //出队列 queue.pop(); //一层的元素个数减1,一层遍历完高度加1 rowCount --; if(node->left){ queue.push(node->left); } if(node->right){ queue.push(node->right); } //一层遍历完 if(rowCount == 0){ //高度加1 height++; //下一层元素个数 rowCount = queue.size(); } } return height; }};
【栈】
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: int maxDepth(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function if(root == NULL) return 0; stack<TreeNode*> S; int maxDepth = 0; TreeNode *prev = NULL; S.push(root); while (!S.empty()) { TreeNode *curr = S.top(); if (prev == NULL || prev->left == curr || prev->right == curr) { if (curr->left) S.push(curr->left); else if (curr->right) S.push(curr->right); } else if (curr->left == prev) { if (curr->right) S.push(curr->right); } else { S.pop(); } prev = curr; if (S.size() > maxDepth) maxDepth = S.size(); } return maxDepth; } };
【测试】
/********************************** 日期:2013-12-08* 作者:SJF0115* 题目: 104.Maximum Depth of Binary Tree* 来源:http://oj.leetcode.com/problems/maximum-depth-of-binary-tree/* 结果:AC* 来源:LeetCode* 总结:**********************************/#include <iostream>#include <malloc.h>#include <stdio.h>using namespace std;typedef struct TreeNode{ int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {}}TreeNode,*BiTree;//按先序序列创建二叉树int CreateBiTree(BiTree &T){int data;//按先序次序输入二叉树中结点的值,‘-1’表示空树scanf("%d",&data);if(data == -1){T = NULL;}else{T = (BiTree)malloc(sizeof(TreeNode));//生成根结点T->val = data;//构造左子树CreateBiTree(T->left);//构造右子树CreateBiTree(T->right);}return 0;}//二叉树最大深度(递归)int maxDepth(TreeNode *root) { if(root == NULL){ return 0; } int left = maxDepth(root->left); int right = maxDepth(root->right); return 1 + max(left,right);}int main() { int i,n; BiTree T = NULL; CreateBiTree(T); printf("%d\n",maxDepth(T)); return 0;}
- [LeetCode]104.Maximum Depth of Binary Tree
- 【LeetCode】104.Maximum Depth of Binary Tree
- [Leetcode] 104. Maximum Depth of Binary Tree
- LeetCode --- 104. Maximum Depth of Binary Tree
- LeetCode 104. Maximum Depth of Binary Tree
- leetCode 104. Maximum Depth of Binary Tree
- 104. Maximum Depth of Binary Tree LeetCode
- 104. Maximum Depth of Binary Tree LeetCode
- LeetCode 104. Maximum Depth of Binary Tree
- leetcode:104. Maximum Depth of Binary Tree
- LeetCode 104. Maximum Depth of Binary Tree
- [LeetCode]104. Maximum Depth of Binary Tree
- leetcode 104. Maximum Depth of Binary Tree
- LeetCode 104. Maximum Depth of Binary Tree
- 【LeetCode】104. Maximum Depth of Binary Tree
- LeetCode#104. Maximum Depth of Binary Tree
- Leetcode 104. Maximum Depth of Binary Tree
- LeetCode *** 104. Maximum Depth of Binary Tree
- poj 3281 最大流
- Java 内存溢出(java.lang.OutOfMemoryError)问题总结
- WCF学习笔记之重载
- UML类图几种关系的总结
- myEclipse安装和使用
- [LeetCode]104.Maximum Depth of Binary Tree
- css+div
- Socket通信中的多进程编程实例
- Linux系统启动的标准流程
- python 核心模块之pickle和cPickle讲解
- 笔记7 (复制控制)
- 例说SQL SERVER 2008内置函数之字符串函数
- Shell脚本学习之sed详解
- 深入理解Qt多线程