Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.题意:知道二叉树的前序遍历和中...
分类:
其他好文 时间:
2014-09-29 23:47:51
阅读次数:
265
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-09-29 19:55:01
阅读次数:
169
第一种方法是Morris Traversal
是O(n)时间复杂度,且不需要额外空间的方法。缺点是需要修改树。
通过将叶子节点的right指向其中序后继。
代码如下
vector inorderTraversal(TreeNode *root) {
vector res;
TreeNode * cur = root;
TreeNode...
分类:
其他好文 时间:
2014-09-27 22:46:50
阅读次数:
195
同num8一样,此题考查的是二叉树的中序遍历,即先左子树再节点再右子树、
使用迭代法时,采用将节点和左子树均压入栈的方法,当左子树为NULL时,将top节点弹出,并存入结果列表,将next指针指向该节点的右节点
代码如下:
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* Tre...
分类:
其他好文 时间:
2014-09-24 20:04:07
阅读次数:
177
题目:Given a binary tree, return the preorder traversal
of its nodes' values.
此题即为二叉树的前序遍历,递归的方法很简单:先节点再左子树再右子树;迭代的方法可以利用栈存储来完成遍历过程。
补充递归与迭代的区别:许多问题是以递归的形式进行解释的,这只是因为它比非递归形式更为清晰。但是,这些问题的迭代往往比递归实现效率更...
分类:
其他好文 时间:
2014-09-24 11:24:56
阅读次数:
206
程序来自Program Creek前Preorder binary tree traversal is a classic interview problem about trees. The key to solve this problem is to understand the follow...
分类:
编程语言 时间:
2014-09-23 22:37:05
阅读次数:
337
原题:
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 ...
分类:
其他好文 时间:
2014-09-23 20:29:17
阅读次数:
243
[leetcode]Binary Tree Postorder Traversal...
分类:
其他好文 时间:
2014-09-22 14:28:42
阅读次数:
121
[leetcode]Given a binary tree, return the preorder traversal of its nodes' values....
分类:
其他好文 时间:
2014-09-22 13:38:03
阅读次数:
215
二叉树是每个节点最多有两个子树的有序树。二叉树常被用于实现二叉查找树和二叉堆。值得注意的是,二叉树不是树的特殊情形。在图论中,二叉树是一个连通的无环图,并且每一个顶点的度不大于2。有根二叉树还要满足根结点的度不大于2。有了根结点后,每个顶点定义了唯一的根结点,和最多2个子结点。然而,没有足够的信息来区分左结点和右结点。二叉树详细请看本文:二叉树
所谓遍历(Traversal)是指沿着某条搜索路线,依次对树中每个结点均做一次且仅做一次访问。访问结点所做的操作依赖于具体的应用问 题。 遍历是二叉树上最重要的运...
分类:
其他好文 时间:
2014-09-20 16:19:29
阅读次数:
242