235 Lowest Common Ancestor of a Binary Search Tree

来源:互联网 发布:java classloader原理 编辑:程序博客网 时间:2024/04/30 11:58
/**  * Definition for a binary tree node.  * public class TreeNode {  *     int val;  *     TreeNode left;  *     TreeNode right;  *     TreeNode(int x) { val = x; }  * }  */  public class Solution {      public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {          if(root==null || p==null || q==null) return null;          if(Math.max(p.val, q.val) < root.val) {              return lowestCommonAncestor(root.left, p, q);          } else if(Math.min(p.val, q.val) > root.val) {              return lowestCommonAncestor(root.right, p, q);          } else return root;      }  } 
0 0
原创粉丝点击