LeetCode OJ - Maximum Depth of Binary Tree
来源:互联网 发布:佛教用品淘宝店 编辑:程序博客网 时间:2024/05/17 02:40
Given 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.
分析:简单题,两种经典的方法。
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution { int ret = 0;public: int maxDepth(TreeNode *root) { if(!root) return 0; ret = DFS(root); return ret; } int DFS(TreeNode *root) { if(!root->left && !root->right) { return 1; } int r = 0; int l = 0; if(root->left) { r = DFS(root->left); } if(root->right) { l = DFS(root->right); } return max(r, l) + 1; }};
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution { int ret = 0;public: int maxDepth(TreeNode *root) { if(!root) return 0; DFS(root, 0); return ret; } void DFS(TreeNode *root, int item) { item++; if(!root->left && !root->right) { ret = max(ret, item); return ; } if(root->left) DFS(root->left, item); if(root->right) DFS(root->right, item); }};
0 0
- [LeetCode OJ]Maximum Depth of Binary Tree
- LeetCode OJ:Maximum Depth of Binary Tree
- LeetCode OJ - Maximum Depth of Binary Tree
- [LeetCode OJ]Maximum Depth Of Binary Tree
- LeetCode OJ Maximum Depth of Binary Tree
- <LeetCode OJ> (104 / 111) Maximum & Minimum Depth of Binary Tree
- LeetCode OJ : 104. Maximum Depth of Binary Tree
- LeetCode oj 104. Maximum Depth of Binary Tree(DFS||BFS)
- Leetcode - Tree - Maximum Depth of Binary Tree
- [leetcode][tree] Maximum Depth of Binary Tree
- LeetCode Maximum Depth of Binary Tree
- [Leetcode] Maximum Depth of Binary Tree
- leetcode 24: Maximum Depth of Binary Tree
- [LeetCode] Maximum Depth of Binary Tree
- Leetcode 104 Maximum Depth of Binary Tree
- 【leetcode】Maximum Depth of Binary Tree
- [leetcode]Maximum Depth of Binary Tree
- [LeetCode]Maximum Depth of Binary Tree
- AndroidManifest.xml简析
- UVA 10780 Again Prime? No time
- iOS学习笔记(一)背景
- NYOJ 220 推桌子
- Ruby基础知识-条件判断语句、case when
- LeetCode OJ - Maximum Depth of Binary Tree
- PHP5 session 详解【经典】
- 上层得到底层数据的一种回调机制-Camera应用程序获得拍照的图片
- 算法导论,9.3,最坏情线性时间的选择算法
- This file requires _WIN32_WINNT to be #defined at least to 0x0403
- Ubuntu12.04下以tar.gz包方式安装Go语言
- 浅析——ContentProvider的使用
- 汇编入门学习笔记 (八)—— 转移指令
- Solr4.7源码分析-启动篇(四)