购物商城网站的运营,下载应用市场软件,注册人力资源公司大概要多少钱,品牌网站建设 蝌蚪5小做不来#xff0c;我做中等题基本上都是没有思路#xff0c;这里需要先遍历祖先节点#xff0c;那必然用先序遍历#xff0c;这题还是官方题解容易理解#xff0c;第二火的题解反而把我弄得脑袋昏昏的。 class Solution { TreeNode ans; public TreeNode lowestCommonAnce…做不来我做中等题基本上都是没有思路这里需要先遍历祖先节点那必然用先序遍历这题还是官方题解容易理解第二火的题解反而把我弄得脑袋昏昏的。 class Solution { TreeNode ans; public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { dfs(root,p,q); return ans; } boolean dfs(TreeNode root,TreeNode p, TreeNode q){ // 判断该树有没有p或q节点有则为true if(rootnull) return false; boolean ldfs(root.left,p,q); boolean rdfs(root.right,p,q); if((rootp||rootq)lfalserfalse){ // 注意要有lfalserfalse防止遇到p或q直接退出函数还要看其子树还有没有p或q。 return true; } if((rootp||rootq)(ltrue||rtrue)){ ansroot; return true; }else if((ltruertrue)){ ansroot; return true; }else { return l||r; } } }