03 2019 档案

摘要:Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path betwe 阅读全文
posted @ 2019-03-15 16:16 MarkLeeBYR 阅读(78) 评论(0) 推荐(0) 编辑
摘要:Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two orzero sub-node. I 阅读全文
posted @ 2019-03-15 16:13 MarkLeeBYR 阅读(85) 评论(0) 推荐(0) 编辑
摘要:Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might 阅读全文
posted @ 2019-03-15 16:12 MarkLeeBYR 阅读(104) 评论(0) 推荐(0) 编辑
摘要:Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target. S 阅读全文
posted @ 2019-03-15 16:11 MarkLeeBYR 阅读(105) 评论(0) 推荐(0) 编辑
摘要:Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as the absolute difference between the sum of all left subt 阅读全文
posted @ 2019-03-15 16:09 MarkLeeBYR 阅读(80) 评论(0) 推荐(0) 编辑
摘要:Find the sum of all left leaves in a given binary tree. Solution1 :Recursive递归,注意:left leaves是左子叶,终端节点才是叶节点。所以只有9和15计算在内 class Solution { public int s 阅读全文
posted @ 2019-03-15 16:08 MarkLeeBYR 阅读(166) 评论(0) 推荐(0) 编辑
摘要:Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array. class Solution { public List<Double> avera 阅读全文
posted @ 2019-03-15 16:05 MarkLeeBYR 阅读(114) 评论(0) 推荐(0) 编辑
摘要:Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are n 阅读全文
posted @ 2019-03-15 16:03 MarkLeeBYR 阅读(100) 评论(0) 推荐(0) 编辑
摘要:You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way. The null node needs to be rep 阅读全文
posted @ 2019-03-15 16:02 MarkLeeBYR 阅读(69) 评论(0) 推荐(0) 编辑
摘要:* Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } 阅读全文
posted @ 2019-03-15 16:01 MarkLeeBYR 阅读(117) 评论(0) 推荐(0) 编辑
摘要:Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and node values with a subtree of s. A subtree of s is a 阅读全文
posted @ 2019-03-15 16:00 MarkLeeBYR 阅读(74) 评论(0) 推荐(0) 编辑
摘要:Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. Assume a BST is define 阅读全文
posted @ 2019-03-15 15:55 MarkLeeBYR 阅读(80) 评论(0) 推荐(0) 编辑
摘要:You are given a binary tree in which each node contains an integer value. Find the number of paths that sum to a given value. The path does not need t 阅读全文
posted @ 2019-03-15 15:54 MarkLeeBYR 阅读(118) 评论(0) 推荐(0) 编辑
摘要:Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: Solution1: class Solution { public List<String> bina 阅读全文
posted @ 2019-03-15 15:45 MarkLeeBYR 阅读(84) 评论(0) 推荐(0) 编辑
摘要:For example, the lowest common ancestor (LCA) of nodes 2 and 8 is 6. Another example is LCA of nodes 2 and 4 is 2, since a node can be a descendant of 阅读全文
posted @ 2019-03-15 15:40 MarkLeeBYR 阅读(100) 评论(0) 推荐(0) 编辑
摘要:Solution1: class Solution { public TreeNode invertTree(TreeNode root) { if (root == null) return null; TreeNode tmp = root.left; root.left = invertTre 阅读全文
posted @ 2019-03-15 15:38 MarkLeeBYR 阅读(100) 评论(0) 推荐(0) 编辑
摘要:Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. F 阅读全文
posted @ 2019-03-15 15:33 MarkLeeBYR 阅读(89) 评论(0) 推荐(0) 编辑
摘要:Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest l 阅读全文
posted @ 2019-03-15 15:32 MarkLeeBYR 阅读(80) 评论(0) 推荐(0) 编辑
摘要:Given an array where elements are sorted in ascending order, convert it to a height balanced BST. class Solution { public TreeNode sortedArrayToBST(in 阅读全文
posted @ 2019-03-15 15:30 MarkLeeBYR 阅读(78) 评论(0) 推荐(0) 编辑
摘要:Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the dept 阅读全文
posted @ 2019-03-15 15:30 MarkLeeBYR 阅读(70) 评论(0) 推荐(0) 编辑
摘要:Solution1:广度优先 class Solution { public List<List<Integer>> levelOrderBottom(TreeNode root) { Queue<TreeNode> q = new LinkedList<>(); List<List<Integer 阅读全文
posted @ 2019-03-15 15:29 MarkLeeBYR 阅读(74) 评论(0) 推荐(0) 编辑
摘要:Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest l 阅读全文
posted @ 2019-03-15 15:21 MarkLeeBYR 阅读(48) 评论(0) 推荐(0) 编辑
摘要:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). Solution1: class Solution { public boolean isSymmetric( 阅读全文
posted @ 2019-03-15 15:20 MarkLeeBYR 阅读(89) 评论(0) 推荐(0) 编辑
摘要:Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical a 阅读全文
posted @ 2019-03-15 15:17 MarkLeeBYR 阅读(86) 评论(0) 推荐(0) 编辑
摘要:Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes. Solution 1://先遍历左节点然后把左节点设为 阅读全文
posted @ 2019-03-15 15:16 MarkLeeBYR 阅读(142) 评论(0) 推荐(0) 编辑
摘要:Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the roo 阅读全文
posted @ 2019-03-15 15:13 MarkLeeBYR 阅读(151) 评论(0) 推荐(0) 编辑
摘要:Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only 阅读全文
posted @ 2019-03-15 15:11 MarkLeeBYR 阅读(154) 评论(0) 推荐(0) 编辑
摘要://Solution 1:递归 public List<Integer> inorderTraversal(TreeNode root) { List<Integer> list =new ArrayList<>(); addNode(list, root); return list;}privat 阅读全文
posted @ 2019-03-15 15:02 MarkLeeBYR 阅读(66) 评论(0) 推荐(0) 编辑
摘要:Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example,Given n = 3, there are a total of 5 unique BST' 阅读全文
posted @ 2019-03-15 14:59 MarkLeeBYR 阅读(78) 评论(0) 推荐(0) 编辑
摘要:Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should 阅读全文
posted @ 2019-03-15 14:49 MarkLeeBYR 阅读(87) 评论(0) 推荐(0) 编辑
摘要:Solution 1: public List<List<Integer>> levelOrder(TreeNode root) { Queue<TreeNode> queue = new LinkedList<>(); List<List<Integer>> wrapList = new Link 阅读全文
posted @ 2019-03-15 14:43 MarkLeeBYR 阅读(82) 评论(0) 推荐(0) 编辑
摘要:Solution 1: public class Solution { public List<List<Integer>> zigzagLevelOrder(TreeNode root) { List<List<Integer>> sol = new ArrayList<>(); travel(r 阅读全文
posted @ 2019-03-15 14:35 MarkLeeBYR 阅读(84) 评论(0) 推荐(0) 编辑
摘要:Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 见剑指offer重建二叉树 阅读全文
posted @ 2019-03-15 14:26 MarkLeeBYR 阅读(95) 评论(0) 推荐(0) 编辑
摘要:Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. class Soluti 阅读全文
posted @ 2019-03-15 14:20 MarkLeeBYR 阅读(69) 评论(0) 推荐(0) 编辑
摘要:given a binary tree, return the preorder traversal of its nodes' values. Solution 1://非递归 Solution 1://非递归 public class Solution { public List<Integer 阅读全文
posted @ 2019-03-15 14:07 MarkLeeBYR 阅读(72) 评论(0) 推荐(0) 编辑
摘要:Given a binary tree, return the postorder traversal of its nodes' values. Solution 1: class Solution { public List<Integer> postorderTraversal(TreeNod 阅读全文
posted @ 2019-03-15 13:33 MarkLeeBYR 阅读(93) 评论(0) 推荐(0) 编辑
摘要:Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. class Solution { public TreeNode sortedL 阅读全文
posted @ 2019-03-15 13:32 MarkLeeBYR 阅读(64) 评论(0) 推荐(0) 编辑
摘要://例如根节点为1,左2右3 class Solution { TreeNode prev = null; public void flatten(TreeNode root) {//先把最大的数设在root.right,然后剩下的数一个个往里加 if (root == null) return; 阅读全文
posted @ 2019-03-15 13:29 MarkLeeBYR 阅读(78) 评论(0) 推荐(0) 编辑
摘要:Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set toNULL. Initially, all next 阅读全文
posted @ 2019-03-15 13:25 MarkLeeBYR 阅读(68) 评论(0) 推荐(0) 编辑
摘要://按层从左往右检索 //按层从左往右检索 //按层从左往右检索 //按层从左往右检索 public class Solution { public void connect(TreeLinkNode root) { while (root != null) { TreeLinkNode tempC 阅读全文
posted @ 2019-03-15 12:02 MarkLeeBYR 阅读(63) 评论(0) 推荐(0) 编辑
摘要:class Solution { public int sumNumbers(TreeNode root) { return sum(root, 0); } private int sum(TreeNode n, int s) { if (n == null) return 0; if (n.rig 阅读全文
posted @ 2019-03-15 11:57 MarkLeeBYR 阅读(79) 评论(0) 推荐(0) 编辑
摘要:给定一个二叉树和所要查找的两个节点,找到两个节点的最近公共父亲节点(LCA)。比如,节点5和1的LCA是3,节点5和4的LCA是5。 class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, Tr 阅读全文
posted @ 2019-03-15 11:48 MarkLeeBYR 阅读(82) 评论(0) 推荐(0) 编辑
摘要:public class Solution { public TreeNode insertNode(TreeNode root, TreeNode node) { if(root == null){ return node; } if(root.val > node.val){ //这个树里面没有 阅读全文
posted @ 2019-03-15 11:45 MarkLeeBYR 阅读(153) 评论(0) 推荐(0) 编辑