随笔分类 -  Leetcode | Tree

摘要:问题描述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 on... 阅读全文
posted @ 2015-08-12 10:43 Chapter 阅读(121) 评论(0) 推荐(0)
摘要:Problem I: Lowest Common Ancestor of a Binary Search TreeGiven a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in... 阅读全文
posted @ 2015-08-12 10:37 Chapter 阅读(190) 评论(0) 推荐(0)
摘要:问题描述Given preorder and inorder traversal of a tree, construct the binary tree.解决思路首先确定根节点,然后确定左右子树的节点数目。依次递归即可。假设输入的序列均合法。程序public class BuildTreeFrom... 阅读全文
posted @ 2015-07-18 11:04 Chapter 阅读(174) 评论(0) 推荐(0)
摘要:问题描述Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.Callingnext()will return the ... 阅读全文
posted @ 2015-07-17 18:34 Chapter 阅读(132) 评论(0) 推荐(0)
摘要:问题描述Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next po... 阅读全文
posted @ 2015-07-17 17:53 Chapter 阅读(175) 评论(0) 推荐(0)
摘要:问题描述Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.解决思路递归思路。中序遍历的过程中,第一个违反顺序的节点一定是错... 阅读全文
posted @ 2015-07-17 12:08 Chapter 阅读(284) 评论(0) 推荐(0)
摘要:问题描述给定一棵BST和一个数K,找出BST中与K值最为接近的数。解决思路递归。返回当前节点和目标值的差值、左子树节点和目标值的差值以及右子树节点和目标值的差值中,最小的那个所对应节点值。程序public class FindClosestInBST { public TreeNode findCl... 阅读全文
posted @ 2015-07-17 11:34 Chapter 阅读(643) 评论(0) 推荐(0)
摘要:问题描述Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to thedefinition of LCA on Wikiped... 阅读全文
posted @ 2015-07-12 20:40 Chapter 阅读(236) 评论(0) 推荐(0)
摘要:Given a complete binary tree, count the number of nodes.Definition of a complete binary tree from Wikipedia:In a complete binary tree every level, exc... 阅读全文
posted @ 2015-06-25 11:25 Chapter 阅读(164) 评论(0) 推荐(0)