Lowest Common Ancestor of a Binary Search Tree
来源:互联网 发布:家暴 知乎 编辑:程序博客网 时间:2024/06/05 02:04
/** * 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) { return null; } if (root.val > p.val && root.val > q.val) { return lowestCommonAncestor(root.left, p, q); } else if (root.val < p.val && root.val < q.val) { return lowestCommonAncestor(root.right, p, q); } else { return root; } } // public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { // if (root == null || root == p || root == q) { // return root; // } // TreeNode ln = lowestCommonAncestor(root.left, p, q); // TreeNode rn = lowestCommonAncestor(root.right, p, q); // if (ln != null && rn != null) { // return root; // } // if (ln != null) { // return ln; // } // if (rn != null) { // return rn; // } // return null; // }}
0 0
- Lowest Common Ancestor of a Binary Search Tree
- leetcode 235: Lowest Common Ancestor of a Binary Search Tree
- Lowest Common Ancestor of a Binary Search Tree (235)
- Lowest Common Ancestor of a Binary Search Tree
- Lowest Common Ancestor of a Binary Search Tree
- Lowest Common Ancestor of a Binary Search Tree leetcode235
- LeetCode 235. Lowest Common Ancestor of a Binary Search Tree
- Lowest Common Ancestor of a Binary Search Tree
- Lowest Common Ancestor of a Binary Search Tree
- leetcode: Lowest Common Ancestor of a Binary Search Tree
- [leetcode][dfs] Lowest Common Ancestor of a Binary Search Tree
- [leetcode] 235.Lowest Common Ancestor of a Binary Search Tree
- Lowest Common Ancestor of a Binary Search Tree
- Lowest Common Ancestor of a Binary Search Tree
- 【LeetCode】235 Lowest Common Ancestor of a Binary Search Tree
- [LeetCode]235.Lowest Common Ancestor of a Binary Search Tree
- leetcode 235 Lowest Common Ancestor of a Binary Search Tree
- [leetcode] Lowest Common Ancestor of a Binary Search Tree
- java web项目开发环境
- 关于C# Dockpanel的一些入门的基本操作
- 怎么设置 运行bat 文件不弹出CMD窗口
- android 打包成jar包(转)
- SQL的基本操作
- Lowest Common Ancestor of a Binary Search Tree
- ObjectARX简单功能实现
- Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'to-many key not all
- bzoj 2440: [中山市选2011]完全平方数
- JVM概念之Java对象的大小与引用类型
- Linux共享内存的内核实现
- 选择排序----堆排序----java实现
- C语言入门(三)菜单设置
- java web项目部署方式