![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Tree
文章平均质量分 61
再见小小ronnie
这个作者很懒,什么都没留下…
展开
-
Leetcode 101. Symmetric Tree
public class Solution { public boolean isSymmetric(TreeNode root) { if (root == null) return true; return helper(root.left, root.right); } public static boolean helper转载 2017-01-15 11:28:05 · 138 阅读 · 0 评论 -
Leetcode 235. Lowest Common Ancestor of a Binary Search Tree
// Recursive approachpublic class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if (root.val > p.val && root.val > q.val) return lo原创 2017-03-30 02:53:03 · 180 阅读 · 0 评论 -
Leetcode 230. Kth Smallest Element in a BST
O(n).public class Solution { public int kthSmallest(TreeNode root, int k) { // save the pre-order of the tree // from left to right, return kth element in the list List r原创 2017-01-27 04:33:24 · 135 阅读 · 0 评论 -
222. Count Complete Tree Nodes
T(n) = 2T(n/2) + logn = O(logn*logn)public class Solution { public int countNodes(TreeNode root) { if (root == null) return 0; // count the height for the left most leaf and righ原创 2017-01-27 04:19:39 · 159 阅读 · 0 评论 -
Leetcode 436. Find Right Interval
TreeMap is a Red-Black tree based NavigableMap implementation. The map is sorted according to the natural ordering of its keys.The time complexity for containsKey, get, put and removeoperations ar原创 2017-01-26 09:32:11 · 243 阅读 · 0 评论 -
Leetcode 108. Convert Sorted Array to Binary Search Tree
public class Solution { public TreeNode sortedArrayToBST(int[] nums) { return helper(0, nums.length-1, nums); } // requires a balanced binary search tree, every time choosing原创 2017-01-20 10:45:56 · 145 阅读 · 0 评论 -
Leetcode 98. Validate Binary Search Tree
O(n) define minimum and maximum every recur.public class Solution { public boolean isValidBST(TreeNode root) { return helper(root, null, null); } public static boolean helpe原创 2017-01-16 10:31:45 · 132 阅读 · 0 评论 -
Leetcode 111. Minimum Depth of Binary Tree
public class Solution { public int minDepth(TreeNode root) { if (root == null) return 0; // take care of the trees that only have left or right child if (root.left == null)原创 2017-01-16 09:21:05 · 145 阅读 · 0 评论 -
Leetcode 110. Balanced Binary Tree
public class Solution { public boolean isBalanced(TreeNode root) { // do an inorder traverse if (root == null) return true; if (Math.abs(getHeight(root.left) - getHeight(r原创 2017-01-16 09:06:08 · 130 阅读 · 0 评论 -
Leetcode 112. Path Sum
public class Solution { public boolean hasPathSum(TreeNode root, int sum) { int tmp = 0; return helper(root, tmp, sum); } // inorder traversing public static boole原创 2017-01-16 08:42:11 · 133 阅读 · 0 评论 -
Leetcode 257. Binary Tree Paths
public class Solution { public List binaryTreePaths(TreeNode root) { List ret = new ArrayList<>(); helper(root, ret, ""); return ret; } public static void help原创 2017-01-15 13:44:27 · 153 阅读 · 0 评论 -
Leetcode 104. Maximum Depth of Binary Tree
public class Solution { public int maxDepth(TreeNode root) { if (root == null) return 0; return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; }}原创 2017-01-15 11:43:18 · 171 阅读 · 0 评论 -
Leetcode 236. Lowest Common Ancestor of a Binary Tree
public class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if (root == null || root == p || root == q) return root; TreeNode转载 2017-03-30 08:10:38 · 177 阅读 · 0 评论