每天刷個演算法題20160518:非遞迴二叉樹遍歷
版權所有。所有權利保留。
歡迎轉載,轉載時請註明出處:
http://blog.csdn.net/xiaofei_it/article/details/51502254
為了防止思維僵化,每天刷個演算法題。已經刷了幾天了,現在貼點程式碼。
2002年我初中二年級,開始學習BASIC語言。2004年中考之後,開始參加NOIP,系統學習演算法。一直非常喜歡演算法,但工作後幾乎不再碰這些東西。現在準備重新撿起來。
我已經建了一個開源專案,每天的題目都在裡面:
https://github.com/Xiaofei-it/Algorithms
絕大部分演算法都是我自己寫的,沒有參考網上通用程式碼。讀者可能會覺得有的程式碼晦澀難懂,因為那是我自己的理解。
最近幾天都是在寫一些原來的東西,大多數是非遞迴。以後準備刷點DP、貪心之類的題。
這裡貼一個遞迴與非遞迴二叉樹遍歷。
前序遍歷
/**
*
* Copyright 2016 Xiaofei
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package xiaofei.algorithm;
/**
* Created by Xiaofei on 16/5/18.
*/
public class PreOrderTraversal {
public static void traversalRecursively(BinaryTreeNode root) {
if (root == null) {
return;
}
System.out.print(root.data + " ");
traversalRecursively(root.left);
traversalRecursively(root.right);
}
public static void traversalCorecursively(BinaryTreeNode root) {
class StackElement {
BinaryTreeNode node;
boolean leftVisited;
boolean rightVisited;
}
StackElement[] stack = new StackElement[100];
for (int i = 0; i < 100; ++i) {
stack[i] = new StackElement();
}
int top = -1;
stack[++top] = new StackElement();
stack[top].node = root;
while (top >= 0) {
BinaryTreeNode node = stack[top].node;
if (!stack[top].leftVisited) {
System.out.print(node.data + " ");
stack[top].leftVisited = true;
if (node.left != null) {
stack[++top] = new StackElement();
stack[top].node = node.left;
}
} else if (!stack[top].rightVisited) {
stack[top].rightVisited = true;
if (node.right != null) {
stack[++top] = new StackElement();
stack[top].node = node.right;
}
} else {
--top;
}
}
}
}
中序遍歷
/**
*
* Copyright 2016 Xiaofei
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package xiaofei.algorithm;
/**
* Created by Xiaofei on 16/5/18.
*/
public class InOrderTraversal {
public static void traversalRecursively(BinaryTreeNode root) {
if (root == null) {
return;
}
traversalRecursively(root.left);
System.out.print(root.data + " ");
traversalRecursively(root.right);
}
public static void traversalCorecursively(BinaryTreeNode root) {
class StackElement {
BinaryTreeNode node;
boolean leftVisited;
boolean rightVisited;
}
StackElement[] stack = new StackElement[100];
for (int i = 0; i < 100; ++i) {
stack[i] = new StackElement();
}
int top = -1;
stack[++top] = new StackElement();
stack[top].node = root;
while (top >= 0) {
BinaryTreeNode node = stack[top].node;
if (!stack[top].leftVisited) {
stack[top].leftVisited = true;
if (node.left != null) {
stack[++top] = new StackElement();
stack[top].node = node.left;
}
} else if (!stack[top].rightVisited) {
System.out.print(node.data + " ");
stack[top].rightVisited = true;
if (node.right != null) {
stack[++top] = new StackElement();
stack[top].node = node.right;
}
} else {
--top;
}
}
}
}
後序遍歷
/**
*
* Copyright 2016 Xiaofei
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package xiaofei.algorithm;
/**
* Created by Xiaofei on 16/5/18.
*/
public class PostOrderTraversal {
public static void traversalRecursively(BinaryTreeNode root) {
if (root == null) {
return;
}
traversalRecursively(root.left);
traversalRecursively(root.right);
System.out.print(root.data + " ");
}
public static void traversalCorecursively(BinaryTreeNode root) {
class StackElement {
BinaryTreeNode node;
boolean leftVisited;
boolean rightVisited;
}
StackElement[] stack = new StackElement[100];
for (int i = 0; i < 100; ++i) {
stack[i] = new StackElement();
}
int top = -1;
stack[++top] = new StackElement();
stack[top].node = root;
while (top >= 0) {
BinaryTreeNode node = stack[top].node;
if (!stack[top].leftVisited) {
stack[top].leftVisited = true;
if (node.left != null) {
stack[++top] = new StackElement();
stack[top].node = node.left;
}
} else if (!stack[top].rightVisited) {
stack[top].rightVisited = true;
if (node.right != null) {
stack[++top] = new StackElement();
stack[top].node = node.right;
}
} else {
System.out.print(node.data + " ");
--top;
}
}
}
}
相關文章
- 【刷題】二叉樹非遞迴遍歷二叉樹遞迴
- 遍歷二叉樹-------遞迴&非遞迴二叉樹遞迴
- 二叉樹非遞迴遍歷二叉樹遞迴
- 每天刷個演算法題20160521:二叉樹高度(遞迴與非遞迴)演算法二叉樹遞迴
- 二叉樹建立及遍歷演算法(遞迴及非遞迴)二叉樹演算法遞迴
- 刷題系列 - Python用非遞迴實現二叉樹前序遍歷Python遞迴二叉樹
- 非遞迴先序遍歷二叉樹遞迴二叉樹
- 二叉樹——後序遍歷的遞迴與非遞迴演算法二叉樹遞迴演算法
- 什麼是遍歷二叉樹,JavaScript實現二叉樹的遍歷(遞迴,非遞迴)二叉樹JavaScript遞迴
- 樹3-二叉樹非遞迴遍歷(棧)二叉樹遞迴
- 刷題系列 - Python用非遞迴實現二叉樹後續遍歷Python遞迴二叉樹
- 刷題系列 - Python用非遞迴實現二叉樹中序遍歷Python遞迴二叉樹
- 二叉樹的四種遍歷(遞迴與非遞迴)二叉樹遞迴
- 【資料結構】二叉樹遍歷(遞迴+非遞迴)資料結構二叉樹遞迴
- 二叉樹的非遞迴遍歷寫法二叉樹遞迴
- 樹(2)--二叉樹的遍歷(非遞迴)+線索二叉樹二叉樹遞迴
- 二叉樹的所有遍歷非遞迴實現二叉樹遞迴
- 二叉樹的非遞迴遍歷——java實現二叉樹遞迴Java
- 遍歷二叉樹的遞迴與非遞迴程式碼實現二叉樹遞迴
- 二叉樹的前中後序遍歷(遞迴和非遞迴版本)二叉樹遞迴
- [java] 二叉樹的後序遍歷(遞迴與非遞迴實現)Java二叉樹遞迴
- python實現二叉樹及其七種遍歷方式(遞迴+非遞迴)Python二叉樹遞迴
- 二叉樹的遍歷 → 不用遞迴,還能遍歷嗎二叉樹遞迴
- 重拾演算法(1)——優雅地非遞迴遍歷二叉樹及其它演算法遞迴二叉樹
- 每天刷個演算法題20160525:快速排序的遞迴轉非遞迴解法演算法排序遞迴
- 遍歷二叉樹的迭代和遞迴方法二叉樹遞迴
- Day14 | 二叉樹遞迴遍歷二叉樹遞迴
- 每天刷個演算法題20160524:阿克曼函式的遞迴轉非遞迴解法演算法函式遞迴
- 二叉樹的建立與遍歷(遞迴實現)二叉樹遞迴
- 每天刷個演算法題20160523:騎士巡遊的遞迴轉非遞迴解法演算法遞迴
- 二叉樹建立後,如何使用遞迴和棧遍歷二叉樹?二叉樹遞迴
- 迴圈遍歷二叉樹二叉樹
- 二叉樹迭代器(中序遞迴、前序和後序遍歷)演算法二叉樹遞迴演算法
- 【C++】翻轉二叉樹(遞迴、非遞迴)C++二叉樹遞迴
- 【Java資料結構與演算法筆記(二)】樹的四種遍歷方式(遞迴&非遞迴)Java資料結構演算法筆記遞迴
- 非遞迴實現先序遍歷和中序遍歷遞迴
- 程式碼隨想錄演算法訓練營,9月9日 | 二叉樹遞迴遍歷,迭代遍歷,層序遍歷演算法二叉樹遞迴
- python3實現二叉樹的遍歷與遞迴演算法解析Python二叉樹遞迴演算法