lintcode maximum-depth-of-binary-tree 二叉树的最大深度
来源:互联网 发布:mac opengl include 编辑:程序博客网 时间:2024/05/16 08:59
问题描述
lintcode
笔记
代码
/** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * TreeNode(int val) { * this->val = val; * this->left = this->right = NULL; * } * } */class Solution {public: /** * @param root: The root of binary tree. * @return: An integer */ int maxDepth(TreeNode *root) { // write your code here if (root == NULL) return 0; if (root->left == NULL && root->right != NULL) return maxDepth(root->right) + 1; if (root->left != NULL && root->right == NULL) return maxDepth(root->left) + 1; if (root->left == NULL && root->right == NULL) return 1; if (root->left != NULL && root->right != NULL) { int left = maxDepth(root->left); int right = maxDepth(root->right); if (left > right) return left+1; else return right+1; } }};
二次练习
/** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * TreeNode(int val) { * this->val = val; * this->left = this->right = NULL; * } * } */class Solution {public: /** * @param root: The root of binary tree. * @return: An integer */ int maxDepth(TreeNode *root) { // write your code here if (root == NULL) return 0; return max(maxDepth(root->left), maxDepth(root->right)) + 1; }};
0 0
- lintcode maximum-depth-of-binary-tree 二叉树的最大深度
- Maximum Depth of Binary Tree 二叉树的最大深度
- Maximum Depth of Binary Tree 二叉树的最大深度
- Maximum Depth of Binary Tree 二叉树的最大深度
- Maximum Depth of Binary Tree 二叉树的最大深度
- maximum-depth-of-binary-tree(二叉树的最大深度)
- 【LeetCode】Maximum Depth of Binary Tree (二叉树最大深度)
- Leetcode Maximum Depth of Binary Tree 二叉树最大深度
- Maximum Depth of Binary Tree--二叉树的深度
- 104. Maximum Depth of Binary Tree | 二叉树的深度
- 求二叉树的最大深度 Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree 二叉树的最大深度(重)
- LeetCode OJ 之 Maximum Depth of Binary Tree (二叉树的最大深度)
- [leetcode]Maximum Depth of Binary Tree (求二叉树的最大深度 C语言)
- LinCode Maximum Depth of Binary Tree 二叉树的最大深度
- leetcode系列(28)Maximum Depth of Binary Tree 求二叉树的最大深度
- Maximum Depth of Binary Tree --- 求二叉树的最大深度
- LeetCode 104 Maximum Depth of Binary Tree(二叉树的最大深度)
- 相思似海深,旧事如天远
- ARC forbids explicit message send of 'release' 'release' is unavailable: not available in automatic
- java学习之布局管理器
- 如何以Java实现网页截图技术
- ubuntu下shadowsock搭建
- lintcode maximum-depth-of-binary-tree 二叉树的最大深度
- HDOJ 1060 Leftmost Digit
- jvm是如何加载class
- JNI,JNA,Jnative
- Sqoop Consume Teiid
- Scala
- 敏捷开发和瀑布开发的区别
- 用于网页显示的三方库
- Java 多线程:多线程生成的原因z