[leetcode]236. Lowest Common Ancestor of a Binary Tree树的最小公共祖先
如果一个节点的左右子树上分别有两个节点,那么这棵树是祖先,但是不一定是最小的,但是从下边开始判断,找到后一直返回到上边就是最小的。
如果一个节点的左右子树上只有一个子树上遍历到了节点,那么那个子树可能是一个节点的祖先,也可能是两个节点的祖先,如果是一个节点的祖先,那么公共祖先还在上边,还需要返回结果进行判断,如果是两个节点的祖先,那么最小公共祖先就是这个或者在下边,总之,返回有结果的那个子树就是了。
所以思路就是,往下遍历,直到找到节点然后返回,返回以后判断此时左右子树的情况,根据情况返回根节点的递归结果或者子树的递归结果
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
/*
从底下开始判断,两个节点是不是在左右子树上
*/
if(root==null) return null;
//如果遍历到了某个节点,说明这个节点在这个子树上,返回这个节点
if (root==p||root==q) return root;
//如果还没遍历到,那么公共节点肯定在下边,继续往下寻找左子树和右子树
//返回的两个分别是左、右子树上遍历到的某个节点的祖先
TreeNode lt = lowestCommonAncestor(root.left,p,q);
TreeNode rt = lowestCommonAncestor(root.right,p,q);
//如果左右子树都找到了,那么这个节点就是公共祖先
if (lt!=null&&rt!=null) return root;
//如果有空缺,那么返回那个找到的,这种情况是到现在为止,两个节点已经在一棵子树上了,返回以当前子树为起点的运算结果就行
return (lt==null)?rt:lt;
}
[leetcode]236. Lowest Common Ancestor of a Binary Tree树的最小公共祖先的更多相关文章
- [LeetCode] 236. Lowest Common Ancestor of a Binary Tree 二叉树的最近公共祖先
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- [LeetCode] 236. Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- 236 Lowest Common Ancestor of a Binary Tree 二叉树的最近公共祖先
给定一棵二叉树, 找到该树中两个指定节点的最近公共祖先. 详见:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tre ...
- leetcode@ [236] Lowest Common Ancestor of a Binary Tree(Tree)
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ Given a binary tree, find the ...
- leetcode 236. Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- (medium)LeetCode 236.Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- [leetcode]236. Lowest Common Ancestor of a Binary Tree二叉树最近公共祖先
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. Accordi ...
- [leetcode]236. Lowest Common Ancestor of a Binary Tree 二叉树最低公共父节点
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- LeetCode 236 Lowest Common Ancestor of a Binary Tree 二叉树两个子节点的最低公共父节点
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
随机推荐
- LeetCode 037 Sudoku Solver
题目要求:Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells ...
- select标签
select标签 select 可以创建单选或多选菜单. <!DOCTYPE html> <html> <head> <meta charset=" ...
- 使用django的用户表进行登录管理
改写用户基本表 ... AUTH_USER_MODEL = 'appjwt.User' ... setting.py from django.db import models from django. ...
- 02day
1.python注释(不执行) #:单行注释 ''' '''或者""" """:多行注释 2.python2中文解决方法 #coding=u ...
- PyQt学习随笔:Model/View中设置视图数据项可编辑的方法
在视图对象中调用setEditTriggers方法可以设置视图对象中的数据项是否可编辑以及编辑的触发方法. setEditTriggers方法是QAbstractItemView的方法,语法如下: s ...
- CTFD平台部署自制题目指北(灌题)
给实验室同学搭建的CTFD平台用于内部训练和CTF的校赛,为了循序渐进当然是先内部出一些简单入门的题目,但是网上大部分关于CTFD平台的都只是部署,而关于题目放置的内容却很少,虽然这个过程比较简单,但 ...
- 从Excel获取整列内容进行批量扫描
实习工作原因,需要测试excel表里面ip地址是否存在漏洞,扫了一眼,呕,四五百个IP,光是挨个进行访问,都是一个浩大的工程,所以准备开始摸鱼认真工作 思路是:excel按列提取->将IP按行存 ...
- 熊海CMS xhcms v1.0代码审计
有空的时候就进行小型CMS的代码审计,这次审计的对象是熊海CMS v1.0 本地环境安装好了之后,可以看到提示安装了锁文件 说明重装漏洞应该不会存在了,这时候丢进seay代码审计系统的代码也出结果了, ...
- Mac下查看端口占用情况
为什么 后端开发时,有时会碰到服务无法正常启动,端口被占用.这时需要查看端口占用情况. 是什么 需要用到一些Linux命令. 怎么做 查看占用端口51805的进程 lsof -n -P -i TCP ...
- 团队作业4-Day7
团队作业4-Day7 项目git地址 1. 站立式会议 2. 项目燃尽图 3. 适当的项目截图 4. 代码/文档签入记录(部分) 5. 每人每日总结 吴梓华:今日补充界面小漏洞,修复部分bug 白军强 ...