Tree
文章平均质量分 74
violet_program
这个作者很懒,什么都没留下…
展开
-
LeetCode: Populating Next Right Pointer in Each Node II
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 extr原创 2013-07-12 12:26:53 · 542 阅读 · 0 评论 -
Leetcode: Populating Next Right Pointer in Each Node
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node. If the原创 2013-07-12 11:22:06 · 570 阅读 · 0 评论 -
Leetcode: Balanced Binary Tree
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 ofevery node never differ原创 2013-07-11 09:59:38 · 518 阅读 · 0 评论 -
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./** * Definition for binary tree * public class TreeNode转载 2013-07-08 12:30:20 · 628 阅读 · 0 评论 -
Leetcode: Recover Binary Search Tree
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 devise a原创 2013-06-29 03:46:35 · 559 阅读 · 0 评论 -
Validate 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 node's key.The原创 2013-06-29 02:32:38 · 578 阅读 · 0 评论 -
Leetcode: Unique Binary Search Trees
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.public class Solution { public int numT原创 2013-06-28 10:14:18 · 533 阅读 · 0 评论 -
Leetcode: Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [3,2,1].Note: Recursive solution is tri原创 2013-11-11 10:46:03 · 702 阅读 · 0 评论 -
Leetcode: Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,2,3].Note: Recursive solution is triv原创 2013-11-11 10:45:12 · 819 阅读 · 0 评论 -
Leetcode: Sum Root to Leaf Number
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 number123.Find the total sum原创 2013-07-16 11:09:04 · 544 阅读 · 0 评论 -
Leetcode: Maximum Depth of Binary Tree
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./** * Definition for binary tree * p原创 2013-07-08 09:57:49 · 585 阅读 · 0 评论 -
Leetcode: Unique Binary Search Tree II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n.For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3原创 2013-06-29 01:34:38 · 582 阅读 · 0 评论 -
Binary Tree Level Order Traversal II
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution {转载 2013-03-30 02:10:28 · 563 阅读 · 0 评论 -
Flatten Binary Tree to Linked List
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { publi原创 2013-04-05 22:39:23 · 508 阅读 · 0 评论 -
Scramble String
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.Below is one possible representation of s1 = "great": great / \ gr原创 2013-06-20 12:14:37 · 830 阅读 · 0 评论 -
Leetcode: Path Sum
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 sum原创 2013-07-12 05:26:09 · 557 阅读 · 0 评论 -
Leetcode: Minimum Depth of Binary Tree
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./** * Definition for binary tree * p原创 2013-07-12 04:39:56 · 494 阅读 · 0 评论 -
Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree./** * Definition for binary tree * public class TreeNode转载 2013-07-08 12:26:20 · 741 阅读 · 0 评论 -
Leetcode: Symmetric Tree
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 3But the following原创 2013-07-01 11:40:53 · 932 阅读 · 0 评论 -
Leetcode: Same Tree
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./** * Definit原创 2013-07-01 11:12:09 · 479 阅读 · 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 / 3return [1,3,2].Note: Recursive solution is trivi原创 2013-06-28 05:42:50 · 802 阅读 · 0 评论 -
Binary Tree Max Path Sum
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { int m转载 2013-02-21 12:56:02 · 402 阅读 · 0 评论 -
Leetcode: Binary Tree Maximum Path Sum
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 3Return 6.Pay attenti原创 2013-07-16 05:32:42 · 651 阅读 · 1 评论 -
Leetcode: Path Sum II
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 / \原创 2013-07-12 05:39:38 · 547 阅读 · 0 评论 -
Leetcode: Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST./** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left;原创 2013-07-09 12:15:12 · 513 阅读 · 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.最直接的思维是list赋值给array,O(n).以下为bottom-up递归/** * Definition for singly-linked list. *原创 2013-07-10 05:55:59 · 696 阅读 · 0 评论 -
Leetcode: Binary Tree Zigzag Level Order Traversal
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:Given binary tr原创 2013-07-03 12:07:57 · 577 阅读 · 0 评论 -
Binary Tree Level Order Traversal
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution {转载 2013-03-29 10:42:39 · 536 阅读 · 0 评论