摘要:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++给定一个二叉树和一个和,判断这个树中是否有一个从根到叶子的路径,使其这个路径上面的所有节点值的和为这个给定的值。并且返回所有等于给定值的路径。例如:给定下面的二叉树,并且和为22。 5 / \ 4 8 / / \ 11 13 4 / \ \ 7 2 1返回... 阅读全文
摘要:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++给定一个二叉树和一个和,判断这个树中是否有一个从根到叶子的路径,使其这个路径上面的所有节点值的和为这个给定的值。例如:给定下面的二叉树,并且和为22。 5 / \ 4 8 / / \ 11 13 4 / \ \ 7 2 1返回true,因为这里面存在一个根... 阅读全文
摘要:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++给定一个二叉树,找出他的最小的深度。最小的深度,指的是从根节点到叶子节点的,经历的最多的节点个数。++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Given a binary tree, find its maximum depth.The maximum depth is the number of nodes al 阅读全文
摘要:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++给定一个二叉树,找出他的最小的深度。最小的深度,指的是从根节点到叶子节点的,经历的最小的节点个数。++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Given a binary tree, find its minimum depth.The minimum depth is the number of nodes al 阅读全文
摘要:
利用递归,构造二叉查找树,++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++给一个升序的单向链表,把他转换成一个二叉查找树++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Given a singly linked list where elements are sorted in ascending order, convert it to a height bala 阅读全文
摘要:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++给定一个升序的数组,把他转换成一个高度平衡的二叉查找树++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Given an array where elements are sorted in ascending order, convert it to a height balanced BST.++++++++++++ 阅读全文
摘要:
本质上是递归遍历左右后在与根节点做判断,本质上是后序遍历++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++给你一个二叉树,判断他是否是个有效的二叉查找树(BST)。假定一个BST树按照下面的内容定义:左子树的节点的值都小于父节点。右子树的节点的值都大于父节点。左子树和右子树都得是合法的而二叉查找树。++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Given a bin 阅读全文
摘要:
提到二叉查找树,就得想到二叉查找树的递归定义,左子树的节点值都小于根节点,右子树的节点值都大于根节点。++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++给定一个n,问有多少个不同的二叉查找树,使得每个节点的值为1...n?例如,给定n=3,你的程序应该返回所有的这5个不同的二叉排序树的个数。 1 3 3 2 1 \ / / / \ \ 3 2 1 1 ... 阅读全文
摘要:
当数组为1,2,3,4,...,n时,基于以下原则构建的BST树具有唯一性:以i为根节点的树,其左子树由[1,i-1]构成,其右子树由[i+1, n]构成。我们假定f(i)为以[1,i]能产生的Unique Binary Search Tree的数目,则如果数组为空,毫无疑问,只有一种BST,即空树,f(0)=1。如果数组仅有一个元素1,只有一种BST,单个节点,f(1)=1。如果数组有两个元素1,2,那么有如下两种可能:1 2 \ / 2 1那么分析可得f(2) = f(0) * f(1),1为根的情况 + f(1) * f(0),2为根的情况再看一看3个元素... 阅读全文
摘要:
本质上是二叉树的层次遍历,遍历层次的过程当中把next指针加上去。++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++和问题"Populating Next Right Pointers in Each Node"类似。如果给定的树是任意的二叉树,你先前的方法还能工作吗?笔记:你只能用常量的辅助空间。例如给定的是羡慕的二叉树, 1 / \ 2 3 / \ \ 4 5 7当调用完你的函数后,这个树应该看起来想这... 阅读全文