二叉树的最大深度
2015-08-01 16:13
393 查看
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 == nullptr) {
return 0;//这个就已经包括所有情况了,包括到达叶子节点以及没有根节点的情况
}
int left = maxDepth(root -> left);
int right = maxDepth(root -> right);
return (left > right) ? left + 1 : right +1 ;
}
};
2、求二叉树两个点的最近公共祖先(包括他自己)
/**
* 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 the binary search tree.
* @param A and B: two nodes in a Binary.
* @return: Return the least common ancestor(LCA) of the two nodes.
*/
TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *A, TreeNode *B) {
// write your code here
if (root == nullptr || A == root || B == root) {//对递归基的理解不透彻
return root;
}
TreeNode *left = lowestCommonAncestor(root -> left, A, B);
TreeNode *right = lowestCommonAncestor(root -> right, A, B);
if (left != nullptr && right != nullptr) {
return root;
}
if (right != nullptr) {
return right;
}
if (left != nullptr) {
return left;
}
return nullptr;
}
};
/**
* 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 == nullptr) {
return 0;//这个就已经包括所有情况了,包括到达叶子节点以及没有根节点的情况
}
int left = maxDepth(root -> left);
int right = maxDepth(root -> right);
return (left > right) ? left + 1 : right +1 ;
}
};
2、求二叉树两个点的最近公共祖先(包括他自己)
/**
* 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 the binary search tree.
* @param A and B: two nodes in a Binary.
* @return: Return the least common ancestor(LCA) of the two nodes.
*/
TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *A, TreeNode *B) {
// write your code here
if (root == nullptr || A == root || B == root) {//对递归基的理解不透彻
return root;
}
TreeNode *left = lowestCommonAncestor(root -> left, A, B);
TreeNode *right = lowestCommonAncestor(root -> right, A, B);
if (left != nullptr && right != nullptr) {
return root;
}
if (right != nullptr) {
return right;
}
if (left != nullptr) {
return left;
}
return nullptr;
}
};
相关文章推荐
- SPRING配置事务回滚
- Web列表分页--(单页web)容易上手的方法
- 面向互联网的文本信息处理,语音和音乐搜索技术的发展现状【搜集资料时学习所得,未详加整理】
- HTTP请求与数据下载
- 正式开通CSDN博客!
- Date类型数据的获取与转换与单选框按钮数据的获取
- hdoj.5115 Dire Wolf【区间DP】 2015/08/01
- HDU 5335(2015 ACM多校训练第四场1009)
- hdu-4970--Killing Monsters--数组的巧妙
- java技术经理面试题
- C语言编程入门——数组(上)
- Tom and paper
- Servlet实现文件上传,可多文件上传
- nyoj 九的余数
- Scala入门到精通——第十四节 Case Class与模式匹配(一)
- ise错误Cannot access memory * directly;Cannot assign memory * directly
- 最近点对问题(欧几里得算法)
- 静态初始化与构造函数,先执行静态初始化,再执行构造函数,总是先执行父类的,再执行子类的
- PsCreateSystemThread
- hihoCoder 1051 补提交卡