每天刷个算法题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;
            }
        }

    }

}
时间: 2024-08-06 03:42:04

每天刷个算法题20160518:非递归二叉树遍历的相关文章

非递归二叉树遍历

算法导论:10.4-3 给定一个 n 二叉树结点,写一个 O(n) 非递归处理时间,树中的每个节点keyword出口.堆栈可以用作辅助数据结构. 堆栈实现参考这里. #ifndef _BINARY_TREE_USE_STACK_H_ #define _BINARY_TREE_USE_STACK_H_ /************************************************** 算法导论:10.4-3 给定一个 n 结点的二叉树,写出一个 O(n) 时间的非递归过程.将该

每天刷个算法题20160521:二叉树高度(递归与非递归)

版权所有.所有权利保留. 欢迎转载,转载时请注明出处: http://blog.csdn.net/xiaofei_it/article/details/51502727 为了防止思维僵化,每天刷个算法题.已经刷了几天了,现在发点代码. 我已经建了一个开源项目,每天的题目都在里面: https://github.com/Xiaofei-it/Algorithms 绝大部分算法都是我自己写的,没有参考网上通用代码.读者可能会觉得有的代码晦涩难懂,因为那是我自己的理解. 最近几天都是在写一些原来的东西

每天刷个算法题20160524:阿克曼函数的递归转非递归解法

版权所有.所有权利保留. 欢迎转载,转载时请注明出处: http://blog.csdn.net/xiaofei_it/article/details/51524754 为了防止思维僵化,每天刷个算法题.已经刷了几天了,现在发点代码. 我已经建了一个开源项目,每天的题目都在里面: https://github.com/Xiaofei-it/Algorithms 绝大部分算法都是我自己写的,没有参考网上通用代码.读者可能会觉得有的代码晦涩难懂,因为那是我自己的理解. 最近几天都是在写一些原来的东西

每天刷个算法题20160523:骑士巡游的递归转非递归解法

版权所有.所有权利保留. 欢迎转载,转载时请注明出处: http://blog.csdn.net/xiaofei_it/article/details/51524728 为了防止思维僵化,每天刷个算法题.已经刷了几天了,现在发点代码. 我已经建了一个开源项目,每天的题目都在里面: https://github.com/Xiaofei-it/Algorithms 绝大部分算法都是我自己写的,没有参考网上通用代码.读者可能会觉得有的代码晦涩难懂,因为那是我自己的理解. 最近几天都是在写一些原来的东西

每天刷个算法题20160525:快速排序的递归转非递归解法

版权所有.所有权利保留. 欢迎转载,转载时请注明出处: http://blog.csdn.net/xiaofei_it/article/details/51524798 为了防止思维僵化,每天刷个算法题.已经刷了几天了,现在发点代码. 我已经建了一个开源项目,每天的题目都在里面: https://github.com/Xiaofei-it/Algorithms 绝大部分算法都是我自己写的,没有参考网上通用代码.读者可能会觉得有的代码晦涩难懂,因为那是我自己的理解. 最近几天都是在写一些原来的东西

【LeetCode-面试算法经典-Java实现】【144-Binary Tree Preorder Traversal(二叉树非递归前序遍历)】

[144-Binary Tree Preorder Traversal(二叉树非递归前序遍历)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution

每天刷个算法题20160522:支持各种类型的并查集

版权所有.所有权利保留. 欢迎转载,转载时请注明出处: http://blog.csdn.net/xiaofei_it/article/details/51524671 为了防止思维僵化,每天刷个算法题.已经刷了几天了,现在发点代码. 我已经建了一个开源项目,每天的题目都在里面: https://github.com/Xiaofei-it/Algorithms 绝大部分算法都是我自己写的,没有参考网上通用代码.读者可能会觉得有的代码晦涩难懂,因为那是我自己的理解. 最近几天都是在写一些原来的东西

每天刷个算法题20160526:BFS解决八数码问题(九宫格问题)

版权所有.所有权利保留. 欢迎转载,转载时请注明出处: http://blog.csdn.net/xiaofei_it/article/details/51524864 为了防止思维僵化,每天刷个算法题.已经刷了几天了,现在发点代码. 我已经建了一个开源项目,每天的题目都在里面: https://github.com/Xiaofei-it/Algorithms 绝大部分算法都是我自己写的,没有参考网上通用代码.读者可能会觉得有的代码晦涩难懂,因为那是我自己的理解. 最近几天都是在写一些原来的东西

每天刷个算法题20160519:回溯法解八皇后

版权所有.所有权利保留. 欢迎转载,转载时请注明出处: http://blog.csdn.net/xiaofei_it/article/details/51502622 为了防止思维僵化,每天刷个算法题.已经刷了几天了,现在发点代码. 我已经建了一个开源项目,每天的题目都在里面: https://github.com/Xiaofei-it/Algorithms 绝大部分算法都是我自己写的,没有参考网上通用代码.读者可能会觉得有的代码晦涩难懂,因为那是我自己的理解. 最近几天都是在写一些原来的东西