Tree
文章平均质量分 71
Yingying_code
这个作者很懒,什么都没留下…
展开
-
257. Binary Tree Paths*
Given a binary tree, return all root-to-leaf paths.For example, given the following binary tree: 1 / \2 3 \ 5All root-to-leaf paths are:["1->2->5", "1->3"]public List binar原创 2016-12-21 19:55:23 · 215 阅读 · 0 评论 -
116. Populating Next Right Pointers 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.原创 2016-12-24 18:20:06 · 194 阅读 · 0 评论 -
102. Binary Tree Level Order Traversal*
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 2原创 2016-12-19 23:15:21 · 158 阅读 · 0 评论 -
107. Binary Tree Level Order Traversal II*
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree [3,9,20,null,null,1原创 2016-12-19 23:44:43 · 235 阅读 · 0 评论 -
404. Sum of Left Leaves*
Find the sum of all left leaves in a given binary tree.Example: 3 / \ 9 20 / \ 15 7There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24原创 2016-12-20 23:47:56 · 203 阅读 · 0 评论 -
113. 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 / \原创 2017-02-08 21:03:10 · 238 阅读 · 0 评论 -
437. Path Sum III*
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 to start or end at the root or a leaf, but it原创 2017-01-16 21:38:36 · 213 阅读 · 0 评论 -
110. 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 of every node never diffe原创 2016-12-22 21:49:26 · 220 阅读 · 0 评论 -
236. Lowest Common Ancestor of a 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 two node原创 2016-12-22 23:22:01 · 203 阅读 · 0 评论 -
95. Unique Binary Search Trees II
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 return all 5 unique BST's shown below. 1原创 2017-02-01 21:57:10 · 191 阅读 · 0 评论 -
106. 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.Recursive:public class Solution { public TreeNod原创 2017-01-25 23:06:53 · 245 阅读 · 0 评论 -
98. 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.Th原创 2017-01-29 22:50:58 · 242 阅读 · 0 评论 -
337. House Robber III**
The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour原创 2016-12-24 15:40:08 · 298 阅读 · 0 评论 -
129. Sum Root to Leaf Numbers**
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原创 2016-12-24 15:13:04 · 245 阅读 · 0 评论 -
235. 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 defined betwee原创 2016-12-21 20:35:12 · 204 阅读 · 0 评论 -
112. 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原创 2016-12-21 21:13:55 · 220 阅读 · 0 评论 -
111. Minimum 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.My code:# Definition for a binar原创 2016-12-22 19:54:31 · 190 阅读 · 0 评论 -
100. 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.My code:原创 2016-12-22 20:00:59 · 195 阅读 · 0 评论 -
101. Symmetric Tree*
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree [1,2,2,3,4,4,3] is symmetric: 1 / \ 2 2 / \ / \3 4 4 3原创 2016-12-22 20:42:53 · 215 阅读 · 0 评论 -
103. 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原创 2016-12-22 23:43:59 · 185 阅读 · 0 评论 -
230. Kth Smallest Element in a BST**
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements.Follow up:What if the原创 2016-12-23 21:56:36 · 186 阅读 · 0 评论 -
105. Construct Binary Tree from Preorder and Inorder Traversal**
Given preorder and inorder traversal of a tree, construct the binary treepublic class Solution { public TreeNode buildTree(int[] preorder, int[] inorder) { return helper(0,0,inorder.leng原创 2016-12-24 12:38:32 · 221 阅读 · 0 评论 -
222. Count Complete Tree Nodes**
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, except possibly the last, is completely fille原创 2016-12-24 13:23:10 · 299 阅读 · 0 评论 -
199. Binary Tree Right Side View**
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.For example:Given the following binary tree, 1原创 2016-12-24 14:08:54 · 205 阅读 · 0 评论 -
173. Binary Search Tree Iterator**
mplement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.Calling next() will return the next smallest number in the BST.Note: next()原创 2016-12-24 14:35:54 · 287 阅读 · 0 评论 -
117. Populating Next Right Pointers 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原创 2017-01-30 12:21:02 · 248 阅读 · 0 评论