
DFS
文章平均质量分 77
豆腐脑是咸的
这个作者很懒,什么都没留下…
展开
-
Recover Binary Search Tree (Java)
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note: A solution using O(n) space is pretty straight forward. Could you devis原创 2015-01-29 20:56:46 · 339 阅读 · 0 评论 -
Binary Tree Inorder Traversal (Java)
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: Recursive sol原创 2014-12-31 15:17:16 · 457 阅读 · 0 评论 -
Binary Tree Preorder Traversal (Java)
Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,2,3]. Note: Recursive soluti原创 2014-12-31 15:12:02 · 376 阅读 · 0 评论 -
Flatten Binary Tree to Linked List (Java)
Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 4 6 The flattened tree should look like: 1原创 2014-12-31 10:02:09 · 391 阅读 · 0 评论 -
Convert Sorted Array to Binary Search Tree (Java)
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 利用二分法 Source /** * Definition for binary tree * public class TreeNode { * int val; *原创 2014-12-31 10:25:52 · 368 阅读 · 0 评论 -
Construct Binary Tree from Preorder and Inorder Traversal (Java)
Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 用中序找子树序列个数,在先序中找该长度子序列第一个元素作为子树root Source /** *原创 2014-12-31 13:17:55 · 363 阅读 · 0 评论 -
Binary Tree Maximum Path Sum (Java)
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / \ 2 3 Return 6.原创 2015-02-06 21:02:30 · 487 阅读 · 0 评论 -
Clone Graph (Java)
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for ea原创 2015-01-20 09:33:39 · 418 阅读 · 0 评论 -
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. 和Convert Sorted Array to Binary Search Tree的区别是array可以用二分法,而list没法直接算mid。这时可用双指针,一个走快一个走慢原创 2015-01-21 15:55:53 · 402 阅读 · 0 评论 -
Construct Binary Tree from Inorder and Postorder Traversal (Java)
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 做法参考先序、中序遍历那道题 Source /** * Definition for bina原创 2014-12-31 14:10:00 · 301 阅读 · 0 评论 -
Validate Binary Search Tree (Java)
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 node's key.Th原创 2014-12-30 13:22:19 · 319 阅读 · 0 评论 -
Populating Next Right Pointers in Each Node (Java)
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } Populate each next pointer to point to its next right node.原创 2015-01-18 17:20:48 · 331 阅读 · 0 评论 -
Maximum Depth of Binary Tree (Java)
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 leaf node. Source public int maxDepth(原创 2014-12-24 15:35:26 · 352 阅读 · 0 评论 -
Path Sum II (Java)
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 / \原创 2014-12-30 15:16:25 · 359 阅读 · 0 评论 -
Minimum Depth of Binary Tree (Java)
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 leaf node. Source public int minDepth(Tree原创 2014-12-24 09:54:03 · 324 阅读 · 0 评论 -
Balanced Binary Tree (Java)
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 depth of the two subtrees of every node never diffe原创 2014-12-21 13:22:59 · 538 阅读 · 0 评论 -
Sum Root to Leaf Numbers (Java)
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the tota原创 2014-12-30 10:32:29 · 354 阅读 · 0 评论 -
Same Tree (Java)
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 and the nodes have the same value. Sour原创 2014-12-24 22:09:19 · 310 阅读 · 0 评论 -
Symmetric Tree (Java)
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \ 3 4 4 3 But the f原创 2014-12-24 22:01:14 · 334 阅读 · 0 评论 -
Path Sum (Java)
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. For example: Given the below binary tree and原创 2014-12-23 22:02:18 · 326 阅读 · 0 评论 -
Populating Next Right Pointers in Each Node II (Java)
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous solution still work? Note: You may only use constant原创 2015-01-23 21:49:56 · 347 阅读 · 0 评论