G*0
2 楼
大家都这样么?
我用的是Time Warner cable
我用的是Time Warner cable
g*e
7 楼
不用stack没法做吧
p*2
11 楼
好多简单题也没练过。赶紧写一个练练。
import java.io.*;
import java.util.*;
public class test
{
public static void main(String[] args)
{
new test().run();
}
PrintWriter out = null;
void run()
{
Scanner in = new Scanner(System.in);
out = new PrintWriter(System.out);
Node node4 = new Node(4, null, null);
Node node5 = new Node(5, null, null);
Node node2 = new Node(2, node4, node5);
Node node6 = new Node(6, null, null);
Node node7 = new Node(7, null, null);
Node node3 = new Node(3, node6, node7);
Node root = new Node(1, node2, node3);
HashSet visited = new HashSet();
Stack stack = new Stack();
stack.add(root);
while (!stack.isEmpty())
{
Node node = stack.pop();
if (visited.contains(node))
out.println(node.value);
else
{
visited.add(node);
if (node.right != null)
stack.add(node.right);
stack.add(node);
if (node.left != null)
stack.add(node.left);
}
}
out.close();
}
}
class Node
{
Node left = null;
Node right = null;
int value;
public Node(int v, Node l, Node r)
{
value = v;
left = l;
right = r;
}
}
import java.io.*;
import java.util.*;
public class test
{
public static void main(String[] args)
{
new test().run();
}
PrintWriter out = null;
void run()
{
Scanner in = new Scanner(System.in);
out = new PrintWriter(System.out);
Node node4 = new Node(4, null, null);
Node node5 = new Node(5, null, null);
Node node2 = new Node(2, node4, node5);
Node node6 = new Node(6, null, null);
Node node7 = new Node(7, null, null);
Node node3 = new Node(3, node6, node7);
Node root = new Node(1, node2, node3);
HashSet
Stack
stack.add(root);
while (!stack.isEmpty())
{
Node node = stack.pop();
if (visited.contains(node))
out.println(node.value);
else
{
visited.add(node);
if (node.right != null)
stack.add(node.right);
stack.add(node);
if (node.left != null)
stack.add(node.left);
}
}
out.close();
}
}
class Node
{
Node left = null;
Node right = null;
int value;
public Node(int v, Node l, Node r)
{
value = v;
left = l;
right = r;
}
}
z*8
15 楼
http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Iterator
【在 p*****2 的大作中提到】
:
: 啥意思?跟iterator什么关系?
【在 p*****2 的大作中提到】
:
: 啥意思?跟iterator什么关系?
p*2
16 楼
跟这道题又什么关系呢?
【在 z*********8 的大作中提到】
: http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Iterator
h*e
20 楼
这道题看起来简单,用stack实现起来还是有一些小tricks的,例如
要记录那些结点已经被访问过了,减少不必要的压栈出栈操作等。
下面是我写的代码:
class SNode {
Node node;
boolean visited = false;
public SNode(Node n) {
node = n;
}
}
class BinaryTreeIterator {
Stack stack = new Stack();
public BinaryTreeIterator(Node root) {
SNode s = new SNode(root);
stack.push(s);
}
public boolean hasNext() {
return !stack.empty();
}
public Node next() {
SNode s = stack.pop();
do {
if (s.visited) {
return s.node;
}
// s is not visited yet
if (s.node.right!=null) {
stack.push(new SNode(s.node.right));
}
s.visited = true;
if (s.node.left==null) {
return s.node;
}
stack.push(s);
s = new SNode(s.node.left);
} while (true);
}
}
要记录那些结点已经被访问过了,减少不必要的压栈出栈操作等。
下面是我写的代码:
class SNode {
Node node;
boolean visited = false;
public SNode(Node n) {
node = n;
}
}
class BinaryTreeIterator {
Stack
public BinaryTreeIterator(Node root) {
SNode s = new SNode(root);
stack.push(s);
}
public boolean hasNext() {
return !stack.empty();
}
public Node next() {
SNode s = stack.pop();
do {
if (s.visited) {
return s.node;
}
// s is not visited yet
if (s.node.right!=null) {
stack.push(new SNode(s.node.right));
}
s.visited = true;
if (s.node.left==null) {
return s.node;
}
stack.push(s);
s = new SNode(s.node.left);
} while (true);
}
}
g*e
26 楼
iterative inorder traversal without visited flag
void in_order_traversal_iterative(BinaryTree *root) {
stack s;
BinaryTree *current = root;
while (!s.empty() || current) {
if (current) {
s.push(current);
current = current->left;
} else {
current = s.top();
s.pop();
cout << current->data << " ";
current = current->right;
}
}
}
void in_order_traversal_iterative(BinaryTree *root) {
stack
BinaryTree *current = root;
while (!s.empty() || current) {
if (current) {
s.push(current);
current = current->left;
} else {
current = s.top();
s.pop();
cout << current->data << " ";
current = current->right;
}
}
}
z*8
27 楼
呵呵, leetcode大神那边拿来的吧
【在 g*********e 的大作中提到】
: iterative inorder traversal without visited flag
: void in_order_traversal_iterative(BinaryTree *root) {
: stack s;
: BinaryTree *current = root;
: while (!s.empty() || current) {
: if (current) {
: s.push(current);
: current = current->left;
: } else {
: current = s.top();
【在 g*********e 的大作中提到】
: iterative inorder traversal without visited flag
: void in_order_traversal_iterative(BinaryTree *root) {
: stack
: BinaryTree *current = root;
: while (!s.empty() || current) {
: if (current) {
: s.push(current);
: current = current->left;
: } else {
: current = s.top();
g*e
28 楼
应该是的
p*2
29 楼
这么写还挺容易出错的。
【在 g*********e 的大作中提到】
: iterative inorder traversal without visited flag
: void in_order_traversal_iterative(BinaryTree *root) {
: stack
: BinaryTree *current = root;
: while (!s.empty() || current) {
: if (current) {
: s.push(current);
: current = current->left;
: } else {
: current = s.top();
h*e
30 楼
明白了。我原先的想法是遍历的时候as lazy as possible,只要一找到
合适的结点就立刻返回。这样增加了程序的复杂度,造成有的结点多次入栈
和出栈,虽然两种方法入栈和出栈操作的次数似乎都是一样的。
下面是改写后的程序以及一个实例做比较:
class BinaryTreeIterator {
Stack stack = new Stack();
public BinaryTreeIterator(Node root) {
while (root!=null) {
stack.push(root);
root = root.left;
}
}
public boolean hasNext() {
return !stack.empty();
}
public Node next() {
Node node = stack.pop();
Node current = node.right;
while (current!=null) {
stack.push(current);
current = current.left;
}
return node;
}
}
5
/ \
1 8
\ /
3 6
\
4
最初的方法:
IN: 5 8 5 3 4 8
OUT: 5 3 4 5 8 8
PRINT: 1 3 4 5 6 8
改进后的方法:
IN: 5 1 3 4 8 6
OUT: 1 3 4 5 6 8
PRINT: 1 3 4 5 6 8
合适的结点就立刻返回。这样增加了程序的复杂度,造成有的结点多次入栈
和出栈,虽然两种方法入栈和出栈操作的次数似乎都是一样的。
下面是改写后的程序以及一个实例做比较:
class BinaryTreeIterator {
Stack
public BinaryTreeIterator(Node root) {
while (root!=null) {
stack.push(root);
root = root.left;
}
}
public boolean hasNext() {
return !stack.empty();
}
public Node next() {
Node node = stack.pop();
Node current = node.right;
while (current!=null) {
stack.push(current);
current = current.left;
}
return node;
}
}
5
/ \
1 8
\ /
3 6
\
4
最初的方法:
IN: 5 8 5 3 4 8
OUT: 5 3 4 5 8 8
PRINT: 1 3 4 5 6 8
改进后的方法:
IN: 5 1 3 4 8 6
OUT: 1 3 4 5 6 8
PRINT: 1 3 4 5 6 8
G*s
31 楼
一般面试最好写那个带FLAG版本的,不容出错;
不用FLAG版本的如下,c++版你可以写个类似这个->
void iterativeInorder(Node* root) {
stack nodeStack;
Node *curr = root;
while (true) {
if (curr) {
nodeStack.push(curr);
curr = curr->left;
continue;
}
if (!nodeStack.size()) {
return;
}
curr = nodeStack.top();
nodeStack.pop();
std::cout << "Node data: " << curr->data << std::endl;
curr = curr->right;
}
}
I
【在 z*********8 的大作中提到】
: 我估计不能在内部存一个stack吧, 是不是可以加个parent pointer?如果不能改,怎
: 么做?
不用FLAG版本的如下,c++版你可以写个类似这个->
void iterativeInorder(Node* root) {
stack
Node *curr = root;
while (true) {
if (curr) {
nodeStack.push(curr);
curr = curr->left;
continue;
}
if (!nodeStack.size()) {
return;
}
curr = nodeStack.top();
nodeStack.pop();
std::cout << "Node data: " << curr->data << std::endl;
curr = curr->right;
}
}
I
【在 z*********8 的大作中提到】
: 我估计不能在内部存一个stack吧, 是不是可以加个parent pointer?如果不能改,怎
: 么做?
q*c
32 楼
你这个不是iterator, 而是traversal, 两个是不一样的.Iterator class 要有一个
current state 指向 current node, 每次执行 advance/++ (C++) 或者next(Java)操
作,返回下一个node. 所以inorder iterator 实际上就是找到下一个inorder node.
current state 指向 current node, 每次执行 advance/++ (C++) 或者next(Java)操
作,返回下一个node. 所以inorder iterator 实际上就是找到下一个inorder node.
j*u
34 楼
把traversal 里面的while循环 改成 if 意思就差不多了,既然每次是in-order遍历出
一个点。
一个点。
w*x
35 楼
这题作的不下5遍了, 说实话, 第一次做还挺不容易的:
============带parent===========================
void InOrderPrint(NODE* pRoot)
{
assert(pRoot);
NODE* pCur = pRoot;
while (pCur != NULL)
{
while (pCur->pLft != NULL)
{
cout<nVal< pCur = pCur->pLft;
}
cout<nVal< //The ending condition is tricky but simple
while (pCur != NULL)//use "pCur != NULL" rather than "pCur->pParent
!= NULL"
{
//Don't miss "pCur->pParent != NULL" logic
if (pCur->pParent != NULL && pCur != pCur->pParent->pRgt)
{
pCur = pCur->pParent->pRgt;
break;
}
pCur = pCur->pParent;
}
}
}
==================== use stack ====================================
void _left_push2(NODE* pNode, stack& stk)
{
assert(pNode);
while (pNode != NULL)
{
stk.push(pNode);
pNode = pNode->pLft;
}
}
void InOrderPrint(NODE* pRoot)
{
assert(pRoot);
stack stk;
_left_push2(pRoot, stk);
while (!stk.empty())
{
NODE* pTop = stk.top();
assert(pTop);
stk.pop();
cout<nVal< if (pTop->pRgt != NULL)
_left_push2(pTop->pRgt, stk);
}
}
============带parent===========================
void InOrderPrint(NODE* pRoot)
{
assert(pRoot);
NODE* pCur = pRoot;
while (pCur != NULL)
{
while (pCur->pLft != NULL)
{
cout<
}
cout<
while (pCur != NULL)//use "pCur != NULL" rather than "pCur->pParent
!= NULL"
{
//Don't miss "pCur->pParent != NULL" logic
if (pCur->pParent != NULL && pCur != pCur->pParent->pRgt)
{
pCur = pCur->pParent->pRgt;
break;
}
pCur = pCur->pParent;
}
}
}
==================== use stack ====================================
void _left_push2(NODE* pNode, stack
{
assert(pNode);
while (pNode != NULL)
{
stk.push(pNode);
pNode = pNode->pLft;
}
}
void InOrderPrint(NODE* pRoot)
{
assert(pRoot);
stack
_left_push2(pRoot, stk);
while (!stk.empty())
{
NODE* pTop = stk.top();
assert(pTop);
stk.pop();
cout<
_left_push2(pTop->pRgt, stk);
}
}
相关阅读
论7万买54万房贷款30万没有garbage disposal有没有问题ZT:美国买房详细流程冰箱问题请教:冷冻室关门会有奇怪的冲水还是充气的声音patio的砖缝长草,能用round up么?也问木工大牛们:Miter Saw的选择这个Condo能买吗Ikea的家具,没啥甲醛味儿啊,应该没有毒吧无娃,可撑买个大房子,就是无年轻人的无忧的生活了...有经济实用型洗衣机推荐吗?DIYer们,说说你们出过的是故把?老中在美国,就像中世纪的犹太人Spring TX 5小孩1大人被relative枪杀 (转载)推荐HDTV老中相信实木、砖头、水泥板离墓地的距离买房纠结,这样算不算撑?问问木头大牛们,郭美美与郭伯雄是一家么?你们炒菜还放味精吗?老毛病又犯了,老张有啥妙招吗?