[LeetCode]235. Lowest Common Ancestor of a Binary Search Tree
来源:互联网 发布:多目标优化 百度百科 编辑:程序博客网 时间:2024/05/16 19:22
Problem Description
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”
[https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/]
思路
没看清题目,没注意是BST,所以写了个一般二叉树的。。
具体思路请看[http://blog.csdn.net/u012468264/article/details/50817712]
Code
package q236;import java.util.*;import TreeNode.*;public class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { List<List<TreeNode>> path = new ArrayList<List<TreeNode>>(); List<TreeNode> tmp = new ArrayList<TreeNode>(); tmp.add(root); helper(tmp, root, p, path); helper(tmp, root, q, path); for (int i = path.get(0).size() - 1; i >= 0; i--) { for (int j = path.get(1).size() - 1; j >= 0; j--) { if (path.get(0).get(i) == path.get(1).get(j)) return path.get(0).get(i); } } return null; } public void helper(List<TreeNode> path, TreeNode root, TreeNode target, List<List<TreeNode>> ans) { if (root == target) { ans.add(new ArrayList<TreeNode>(path)); return; } else { if (root.left != null) { path.add(root.left); helper(path, root.left, target, ans); path.remove(path.size() - 1); } if (root.right != null) { path.add(root.right); helper(path, root.right, target, ans); path.remove(path.size() - 1); } } } // public static void main(String[] args) { // Solution s = new Solution(); // Codec c = new Codec(); // TreeNode root = c // .deserialize("6,2,8,0,4,7,9,null,null,3,5,null,null,null,null"); // TreeNode p = c.getNode(root, 2); // TreeNode q = c.getNode(root, 8); // // System.out.println(p.val); // // System.out.println(q.val); // TreeNode ans = s.lowestCommonAncestor(root, p, q); // System.out.println(ans.val); // // }}
0 0
- 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】235. Lowest Common Ancestor of a Binary Search Tree
- 235. Lowest Common Ancestor of a Binary Search Tree LeetCode
- 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 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: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 235. Lowest Common Ancestor of a Binary Search Tree
- leetcode-235. Lowest Common Ancestor of a Binary Search Tree
- 235.[Leetcode]Lowest Common Ancestor of a Binary Search Tree
- 【leetcode】235. Lowest Common Ancestor of a Binary Search Tree
- arduno添加新的avr开发板
- SpringMVC 使用JSR-303进行校验 @Valid
- hiho 1268 九宫 搜索 模拟
- 用Maven构建Hadoop项目(讲的不太详细,有助于理解)
- linux下kill命令小结
- [LeetCode]235. Lowest Common Ancestor of a Binary Search Tree
- 王易见:后电商时代,家电制造业上升还是沉沦?
- jquery在项目中使用积累
- PASSION之安装mysql常见问题
- DOOR SHELL FSR FTR FVR FVM FRR ISUZU 1996
- jQuery v1.11.0的checkbox
- 10032---Node.js实战--欢迎进入Node.js世界
- Android Studio 指定签名证书文件
- 页面置换算法-先进先出(FIFO)