原题地址二叉树的层次遍历。对于每一层,依次把各节点连起来即可。代码: 1 void connect(TreeLinkNode *root) { 2 if (!root) return; 3 4 queue parents; 5 6 parents.push(root); 7 wh...
分类:
其他好文 时间:
2015-01-21 14:57:15
阅读次数:
166
二叉树的前序,中序,后序,层次遍历/** binary tree traversal methods */package dataStructures;public class BinaryTreeTraversal { /** visit method that prints the ele...
分类:
其他好文 时间:
2015-01-15 10:37:52
阅读次数:
230
Given a binary tree
struct TreeLinkNode {
TreeLinkNode *left;
TreeLinkNode *right;
TreeLinkNode *next;
}
Populate each next pointer to point to its next right node. ...
分类:
其他好文 时间:
2015-01-14 18:00:03
阅读次数:
139
Follow up for problem "Populating Next Right Pointers in Each Node".
What if the given tree could be any binary tree? Would your previous solution still work?
Note:
You may only use constant ...
分类:
其他好文 时间:
2015-01-14 17:55:09
阅读次数:
199
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},
...
分类:
其他好文 时间:
2015-01-14 15:36:13
阅读次数:
127
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 ...
分类:
其他好文 时间:
2015-01-14 12:52:20
阅读次数:
176
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
...
分类:
其他好文 时间:
2015-01-14 11:08:38
阅读次数:
226
【题目】
Given a binary tree
struct TreeLinkNode {
TreeLinkNode *left;
TreeLinkNode *right;
TreeLinkNode *next;
}
Populate each next pointer to point to its nex...
分类:
其他好文 时间:
2014-12-24 20:13:35
阅读次数:
151
【题目】
Follow up for problem "Populating Next Right Pointers in Each Node".
What if the given tree could be any binary tree? Would your previous solution still work?
Note:
You may only use...
分类:
其他好文 时间:
2014-12-24 20:13:31
阅读次数:
151
二叉树的遍历方式基本就是前序遍历,中序遍历,后序遍历和层次遍历。从代码的角度来说,前三种最简单的就是用递归了,代码会非常简洁。但是递归有一个缺陷,就是当二叉树的节点非常多的时候,层次深的递归会不停的进行程序的压栈和出栈操作,效率比较低。这里就不写递归算法了,只写四种遍历的非递归算法。
先定义二叉树的节点如下:
/**
* Definition for binary tree
* pub...
分类:
编程语言 时间:
2014-12-15 13:47:08
阅读次数:
305