树型结构问题
ojshilu
https://github.com/lucky521
展开
-
1350 求二叉树的深度
竟然运行一次直接AC!!不可思议!明天再看一下。#include#includeusing namespace std;typedef struct Tnode{ int data; struct Tnode *left; struct Tnode *right;}Tnode;int depth(Tnode *root){ if(root == NULL)原创 2013-07-13 23:05:31 · 710 阅读 · 0 评论 -
根据有序数组构造二叉查找树 Convert Sorted Array to Binary Search Tree
题目源自于leetcode。题目:Given an array where elements are sorted in ascending order, convert it to a height balanced BST.思路:注意这里要的是平衡的BST。递归思想+二分查找法。把正中间的数作为树根,然后左和右两半段作为左子树和右子树。代码:/** * Definit原创 2013-11-08 21:29:21 · 1431 阅读 · 0 评论 -
求二叉树的最小深度 Minimum Depth of Binary Tree
题目源自于leetcode。题目: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.思路:分情况讨论,没有孩子的结点是叶子原创 2013-11-19 16:36:35 · 3498 阅读 · 0 评论 -
把二叉树精简成单链表 Flatten Binary Tree to Linked List
题目: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-03-06 21:47:28 · 1043 阅读 · 0 评论 -
二叉树中增加next指针 Populating Next Right Pointers in Each Node
题目源自于leetcode。题目:Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to原创 2013-11-06 20:58:02 · 1259 阅读 · 0 评论 -
生成所有可能的二叉排序树 Unique Binary Search Trees 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原创 2014-03-21 12:39:21 · 1244 阅读 · 0 评论 -
判断二叉查找树的合法性 Validate Binary Search Tree
题目源自于leetcode。题目: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 th原创 2013-11-19 17:21:23 · 1888 阅读 · 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.思路:二叉查找树的构造往往伴随着二分查找的过程。这是一个单链表而不是数组。二分查找不好用。就用递归吧。每次找到链表的最中间结点, 作为树根节点,左右两侧作为左右孩子。代原创 2014-03-07 10:58:10 · 1338 阅读 · 0 评论 -
求二叉树的最大深度 Maximum Depth of Binary Tree
题目源自于Leetcode。简单递归题,是Leetcode最简单的一道题。代码:class Solution {public: int maxDepth(TreeNode *root) { if(root == NULL) return 0; else { i原创 2013-11-06 13:51:01 · 2033 阅读 · 0 评论 -
二叉树的中序遍历 Binary Tree Inorder Traversal
方法:二叉树的先序遍历形式有递归和非递归两种。递归方式的实现:class Solution {public: vector inorderTraversal(TreeNode *root) { vector out; fun(root, out); return out; } void fun(Tree原创 2014-02-17 14:34:39 · 1023 阅读 · 0 评论 -
判断二叉树是否是水平对称的 Symmetric Tree
题目源自于Leetcode。题目: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原创 2013-11-09 19:33:17 · 3041 阅读 · 2 评论 -
树的最大路径和 Binary Tree Maximum Path Sum
问题:从二叉树中找出一个路径,使其途径的节点权值之和最大。路径是随意的,首末结点在哪儿都可以,也不要求必须经过根节点。由于结点元素有正有负,并非路径越长和越大。思路一:递归思路。找出从左孩子向下到叶子结点的最大单向路径和,找出从右孩子向下到叶结点的最大单向路径和,两者相加再加上根节点本身的值,这是经过根节点的最大路径和。然后分别递归求两个孩子的最大路径和。三个最大路径和从中选最大值。原创 2014-03-04 20:48:13 · 2640 阅读 · 0 评论 -
能构成的二叉查找树的个数 Unique Binary Search Trees
题目源自于Leetcode。题目: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原创 2013-11-07 15:04:57 · 2267 阅读 · 0 评论 -
二叉树的后序遍历 Binary Tree Postorder Traversal
递归方法:class Solution {public: vector postorderTraversal(TreeNode *root) { vector list; if(root == NULL) return list; postorder(root, list); return lis原创 2014-03-09 17:58:33 · 970 阅读 · 0 评论 -
根据中序和后序序列重建二叉树 Construct Binary Tree from Inorder and Postorder Traversal
问题:Given inorder and postorder traversal of a tree, construct the binary tree.参考:根据前序序列和中序序列重建二叉树,http://blog.csdn.net/ojshilu/article/details/11855167,两个问题如出一辙。注意:下面程序并没有做严格的合法性检查。1、递归时两个序列长度总是原创 2014-03-15 12:58:13 · 1133 阅读 · 0 评论 -
判断两棵树是一模一样的 Same Tree
题目源自于Leetcode。简单的递归题。题目: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原创 2013-11-06 14:05:49 · 1351 阅读 · 0 评论 -
寻找树根到叶子的权值之和为sum的路径 Path Sum
题目源自于Leetcode。简单题。本题只要求找是否存在,而没要求返回满足条件的路径。题目: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.Fo原创 2013-11-24 00:44:57 · 1090 阅读 · 0 评论 -
平衡二叉树判定 AVL Balanced Binary Tree
题目源自于leetcode。题目: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原创 2013-11-11 14:43:14 · 1731 阅读 · 0 评论 -
Sum Root to Leaf Numbers
题目源自于leetcode。二叉树递归算数题。题目: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 nu原创 2013-12-06 19:43:28 · 829 阅读 · 0 评论 -
二叉树的层序遍历 改进版 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).思路:由于题目要求输出结果中每一层的结点是分开的。所以需要再层序遍历的过程中加入一个原创 2014-02-21 19:49:06 · 966 阅读 · 0 评论 -
找出二叉树中和为k的所有路径 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.该问题和之前的路径和问题相比,进阶了一步。之前的问题是,判断是否有这样的一条路径,返回布尔值即可。本问题是要找出所有的路径,并且把它们返回。注意:二叉树的结点元素可以是负数的,所有不走到原创 2014-02-23 11:51:14 · 3274 阅读 · 0 评论 -
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).思路:层序遍历的一个小小的变种。偶数层把顺序逆置原创 2014-02-26 21:26:10 · 962 阅读 · 0 评论 -
修复二叉查找树 Recover Binary Search Tree
问题:给出一个有问题的二叉查找树,其中有一对儿结点上的数字弄反了。要求把它俩找出来并恢复到正常的二叉查找树。思路:正常的二叉查找树,中序遍历应该是递增序列。现在有一对数被交换了。那么中序序列中必定先遇到一个比其右边的数要大的数,然后再遇到一个比起左边的数小的数。这两个数就是应该被交换回来的数。比如交换两个不相邻的数 1 2 3 4 5,交换成 1 4 3 2 5,会发现两个降序原创 2014-03-06 20:01:19 · 1472 阅读 · 0 评论 -
二叉树的先序遍历 Binary Tree Preorder Traversal
此问题是数据结构的重要问题,同时题目源自于leetcode。题目:Given a binary tree, return the preorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3retu原创 2013-11-06 19:10:52 · 1274 阅读 · 0 评论 -
根据前序和中序序列重建二叉树 Construct Binary Tree from Preorder and Inorder Traversal
9oj编号1385。题目:根据二叉树的前序遍历序列和中序遍历序列,求得该二叉树的后序遍历序列。注意:有个前提,二叉树中的所有元素都不相同。方法:根据前序序列和中序序列重建二叉树原型,然后进行后序遍历。所以关键就在于重建二叉树。使用递归的方法比较容易。如下图所示,对于二叉树的构建过程也是递归的。首先把当前前序遍历序列的第一个元素作为root结点,并在在中序遍历中找原创 2013-09-20 22:18:02 · 1214 阅读 · 0 评论