![](https://img-blog.csdnimg.cn/20201014180756754.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode之tree
文章平均质量分 56
Shawn_Redemption
这个作者很懒,什么都没留下…
展开
-
【Leetcode】Convert Sorted Array to Binary Search Tree (BST)
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.基本方法就是divide and conquerba原创 2014-11-07 23:13:01 · 274 阅读 · 0 评论 -
【Leetcode】Populating Next Right Point in Each Node II (TL)
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node.原创 2014-11-09 02:55:28 · 351 阅读 · 0 评论 -
【Leetcode】Minimum Depth of Binary Tree (Tree Judge)
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.递归思路,左右叶子记录各自的深度,返回左右叶子的最小深度+1 即原创 2014-11-09 12:02:31 · 345 阅读 · 0 评论 -
【Leetcode】Binary Tree Preorder Traversal (Tree Traversal) (转自Code_Ganker)
跟Binary Tree Inorder Traversal一样,二叉树的先序遍历我们仍然介绍三种方法,第一种是递归,第二种是迭代方法,第三种是用线索二叉树。递归是最简单的方法,算法的时间复杂度是O(n), 而空间复杂度则是递归栈的大小,即O(logn)。代码如下:[java] view plaincopypublic ArrayList p转载 2014-11-11 23:18:48 · 417 阅读 · 0 评论 -
【Leetcode】Binary Tree Inorder Traversal (Tree Traversal)(转自Code_Ganker)
通常,实现二叉树的遍历有两个常用的方法:一是用递归,二是使用栈实现的迭代方法。下面分别介绍。递归应该最常用的算法,相信大家都了解,算法的时间复杂度是O(n), 而空间复杂度则是递归栈的大小,即O(logn)。代码如下: [java] view plaincopypublic ArrayList inorderTraversal(Tre转载 2014-11-11 23:27:49 · 449 阅读 · 0 评论 -
【Leetcode】Binary Tree Postorder Traversal (Tree Traversal)(转自Code_Ganker)
跟Binary Tree Inorder Traversal以及Binary Tree Preorder Traversal一样,二叉树的后序遍历我们还是介绍三种方法,第一种是递归,第二种是迭代方法,第三种是用线索二叉树。 递归还是那么简单,算法的时间复杂度是O(n), 而空间复杂度则是递归栈的大小,即O(logn)。代码如下:[java] view plaincopy转载 2014-11-11 23:21:30 · 421 阅读 · 0 评论 -
【Leetcode】Sum Root to Leaf (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.原创 2014-10-31 10:30:53 · 354 阅读 · 0 评论 -
【Leetcode】Path Sum II (Sum)
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-10-31 09:55:13 · 341 阅读 · 0 评论 -
【Leetcode】Path Sum (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原创 2014-10-31 09:02:05 · 311 阅读 · 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.原创 2014-10-31 11:19:01 · 334 阅读 · 0 评论 -
【Leetcode】Flatten Binary Tree to Linked List (other)
Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1原创 2014-11-09 00:17:36 · 453 阅读 · 0 评论 -
【Leetcode】Validate Bunary Search Tree (Tree Judge)
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-11-09 11:57:43 · 371 阅读 · 0 评论 -
【Leetcode】Symmetric Tree (Tree Judge)
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 f原创 2014-11-09 11:53:35 · 311 阅读 · 0 评论 -
【Leetcode】Binary Tree Level Order Traversal II (Tree Traversal)
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,#,#,15,7},原创 2014-11-08 03:32:46 · 336 阅读 · 0 评论 -
【Leetcode】Binary Tree Zigzag Level Order Traversal (Tree 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原创 2014-11-08 04:23:36 · 243 阅读 · 0 评论 -
【Leetcode】Populating Next Right Point in Each Node (TL)
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node.原创 2014-11-09 02:49:27 · 430 阅读 · 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.这道题和原创 2014-11-08 09:33:03 · 282 阅读 · 0 评论 -
【Leetcode】Same Tree (Tree Judge)
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.原创 2014-11-09 11:51:26 · 340 阅读 · 0 评论 -
【Leetcode】Balanced Binary Tree (Tree Judge)
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-11-09 11:54:33 · 347 阅读 · 0 评论 -
【Leetcode】Maximum Depth of Binary Tree (Tree Judge)
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.原创 2014-11-09 12:01:46 · 306 阅读 · 0 评论 -
【Leetcode】Convert Sorted List to Binary Search Tree (BST)
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.原创 2014-11-08 00:04:42 · 452 阅读 · 0 评论 -
【Leetcode】Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.其实这道题弄清楚分段,思想还是挺简单的preorder的分段:原创 2014-11-08 05:22:01 · 356 阅读 · 0 评论 -
【Leetcode】Binary Tree Level Order Traversal (Tree 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,#,#,15,7}, 3 / \ 9 20原创 2014-11-08 03:07:52 · 284 阅读 · 0 评论