码迷,mamicode.com
首页 >  
搜索关键字:binary trees    ( 12390个结果)
【leetcode】Sum Root to Leaf Numbers
Question: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...
分类:其他好文   时间:2015-04-04 01:08:13    阅读次数:190
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. 根据二叉树的中序遍历和后续遍历结果构造二叉树。思路分析: 这道题和上道题《 Leetcode: Constr...
分类:其他好文   时间:2015-04-04 00:02:22    阅读次数:180
Leetcode: 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. 根据前序遍历和中序遍历结果构造二叉树。思路分析: 分析二叉树前序遍历和中序遍历的结果我们发现: 二叉树中序遍...
分类:其他好文   时间:2015-04-03 22:32:25    阅读次数:234
Reverse Bits
Reverse bits of a given 32 bits unsigned integer.For example, given input 43261596 (represented in binary as00000010100101000001111010011100), return ...
分类:其他好文   时间:2015-04-03 19:11:16    阅读次数:86
leetcode_173_Binary Search Tree Iterator
思路: 这道题想了好久,知道用中序遍历来解决,用一个list将遍历的元素存储起来一下就解决了,但是空间复杂度不行。具体怎么解决,如何控制程序的终止困扰了我好久。知道我想起来至多用O(h) memory,我想到了直接把一趟遍历后返回开始之前的所有元素存储起来不就正好符合题目要求了么,bravo!...
分类:其他好文   时间:2015-04-03 17:34:15    阅读次数:189
leetcode_110_Balanced Binary Tree
思路: 走了好多弯路,最后才发现直接根据定义来就可以了,左右子树的深度不超过1且左右子树都是平衡二叉树,就是这么简洁明快。...
分类:其他好文   时间:2015-04-03 17:32:35    阅读次数:128
leetcode_96_Unique Binary Search Trees
思路: 这一道题呢,刚开始完全没有思路,拿笔在草纸上这样演算,那样演算,但是没有思路。这是一个二叉查找树,所以题眼很可能就是在这里,在网上一查还真是这样。由于这是一棵二叉查找树,所以当一棵树形态固定下来后,该树是唯一的。由于根节点的值大于左子树上的任何一个结点的值,小于右子树上任何一个结点的值,所以,当某个节点被当作树的根节点时,树的形态个数有num(root.left)*num(root.right)个,从num_0,num_1,num_2开始迭代计算即可。...
分类:其他好文   时间:2015-04-03 17:32:04    阅读次数:107
leetcode_105_Construct Binary Tree from Preorder and Inorder Traversal
思路: 1.将中序遍历序列和其对应的下标存储到一个map中,方便下面的查找 2.递归选取前序序列的第一个元素作为树的根节点,然后查找根节点在前序序列中位置inorderIndex,inorderIndex-startInorder可以得到左子树的长度 3.根据左子树的长度和startPreOrder可以求出前序序列中左子树的起始位置 4.从上面可以求出左右子树的前序序列和中序序列的起始位置,递归调用建树过程即可。 PS:其实,对于这道题,有更简单的方法,可根据按前序序列元素出现的顺序依次作为树的根节点进行...
分类:其他好文   时间:2015-04-03 15:17:20    阅读次数:133
leetcode_106_Construct Binary Tree from Inorder and Postorder Traversal
思路: 1.将中序遍历序列和其对应的下标存储到一个map中,方便下面的查找 2.递归选取后序序列的倒数第一个元素作为树的根节点,然后查找根节点在后序序列中位置inorderIndex,endInorder-inorderIndex可以得到右子树的长度 3.根据右子树的长度和endPreOrder可以求出后序序列中右子树的起始位置 4.从上面可以求出左右子树的后序序列和中序序列的起始位置,递归调用建树过程即可。...
分类:其他好文   时间:2015-04-03 15:16:31    阅读次数:89
[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 ...
分类:其他好文   时间:2015-04-03 13:17:49    阅读次数:120
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!