博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] Lowest Common Ancestor of a Binary Tree
阅读量:5748 次
发布时间:2019-06-18

本文共 815 字,大约阅读时间需要 2 分钟。

Well, a follow-up for the problem Lowest Common Ancestor of a Binary Search Tree. However, this time you cannot figure out which subtree the given nodes lie in according to their values. So you need to explicitly find out which subtree they are in. Well, contains a damn clever solution, just in 4 lines with explanations! The code is as follows. It is so concise that I can noly copy it...

1 class Solution {2 public:3     TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {4         if (!root || root == p || root == q) return root;5         TreeNode* left = lowestCommonAncestor(root -> left, p, q);6         TreeNode* right = lowestCommonAncestor(root -> right, p, q);7         return !left ? right : !right ? left : root;8     }9 };

 

转载于:https://www.cnblogs.com/jcliBlogger/p/4643061.html

你可能感兴趣的文章
交互式Ruby解释器:irb
查看>>
一共81个,开源大数据处理工具汇总(上)
查看>>
文件服务器文件夹,共享文件夹及权限迁移
查看>>
玩转grub加密
查看>>
VMware Virtual SAN:见证组件部署逻辑
查看>>
java 内存异常排错
查看>>
调试JS的工具和一些方法
查看>>
第一章 Map
查看>>
Ruby String方法摘要
查看>>
Java 8 基础教程(二)
查看>>
cocos2dx update跳跃函数
查看>>
使用find删除备份数据库的误区
查看>>
Apache - AH00548
查看>>
Spring DATA JPA嵌套子查询
查看>>
build webrtc for android
查看>>
Oracle 索引 详解
查看>>
spring的service类调用自己方法事务无效
查看>>
CentOS “Destination Host Unreachable”问题解决办法
查看>>
java的反射机制浅谈
查看>>
CentOS7中rpm安装MySQL数据库
查看>>