Tree
文章平均质量分 67
ljffdream
这个作者很懒,什么都没留下…
展开
-
【LeetCode】Convert Sorted Array to Binary Search Tree
leetcode原创 2014-11-26 11:05:06 · 290 阅读 · 0 评论 -
【Leetcode】Construct binary tree from inorder and postorder traversal
【题目】 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 【思路】 pretty similar with that转载 2015-09-03 03:10:58 · 392 阅读 · 0 评论 -
【Leetcode】Lowest Common Ancestor of a Binary Search Tree
【题目】 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 defi转载 2015-07-20 21:05:28 · 327 阅读 · 0 评论 -
【Leetcode】Valid Binary Search 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 only nodes with keys less than the nod转载 2015-07-17 23:26:37 · 287 阅读 · 0 评论 -
【Leetcode】Lowest common treenode in binary tree
【题目】 Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between tw转载 2015-07-28 22:41:59 · 483 阅读 · 0 评论 -
【Leetcode】Path sum 2
【题目】 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5转载 2015-07-27 22:44:56 · 319 阅读 · 0 评论 -
[Leetcode]Word Ladder
[题目] Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformation sequence from beginWord to endWord, such that: Only one letter can be changed at a ti转载 2015-07-06 09:12:15 · 473 阅读 · 0 评论 -
【Leetcode】Find the kth in the BST
[题目] [思路] BST的特点就是排序性,左边的节点一定比root小,右边的节点一定比root大。 另外就是二分性。 找第k个,可以先看左边树节点的个数,小于k的话,就保证了肯定是在右边。那么去找右边的k-left. k - left,就变成了新的k. 数root所在的树的节点数。这个就是用递归实现。想法就是,如果root自己本身就是null,那么就return 0; 否则至转载 2015-07-08 21:28:09 · 510 阅读 · 0 评论 -
[Leetcode]Invert Binary Tree
[题目] Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia: This problem was inspired by this original tweet by原创 2015-06-18 16:30:12 · 729 阅读 · 0 评论 -
【Leetcode】Binary Tree Inorder Traversal
【题目】 Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,3,2]. Note: Recursi原创 2014-12-04 16:22:20 · 262 阅读 · 0 评论 -
【知识点】树的遍历
【树的定义】 public class TreeNode{ int val; TreeNode left; TreeNode right; TreeNode(int x){ val=x; } } 【前序遍历】 递归: public void preOrder(TreeNodebiTree){ System.out.printf(biTree.val+""); TreeNode leftTr转载 2014-11-26 16:36:27 · 480 阅读 · 0 评论 -
二叉树遍历神招——morris Traversal
morris traversal 原理很简单,利用所有叶子结点的right 指针,指向其后继结点,组成一个环,在第二次遍历到这个结点时,由于其左子树已经遍历完了,则访问该结点 如下图为morris 的一个示例 【morris中序遍历】 void bst_morris_inorder(struct bst_node *root) { struct bst_n转载 2014-12-04 17:31:43 · 502 阅读 · 0 评论 -
【LeetCode】Unique Binary Search Tree
【题目】 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's. 1 3 3 2原创 2014-12-04 13:37:50 · 330 阅读 · 0 评论 -
【Leetcode】Convert Sorted List to Binary Search Tree
【题目】Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 【思路】这道题目就和array不一样,因为是linkedlist所以,不能随意访问到中间的元素,那么就只能一步一步从头开始顺序访问,从列表到BST,我们能感受到构原创 2014-11-26 12:59:59 · 303 阅读 · 0 评论 -
【Leetcode】Construct Binary Tree From Inorder and Preorder/Postorder Traversal
【题目】 Given preorder and inorder traversal of a tree, construct the binary tree. 【思路】 Hint: A good way to attempt this question is to work backwards. Approach this question by drawing a binary转载 2015-09-03 02:13:50 · 527 阅读 · 0 评论