摘要: 这篇文章展示了用ruby使用不同的方法对二叉树进行遍,并且实现了不同遍历方式的iterator,希望对大家有所帮助。 Talk is cheap, show me the code! 1. Pre-order Traversal (前序遍历先访问当前的节点,然后再访问它的孩子节点) Non-recu 阅读全文
posted @ 2018-06-25 07:12 infinitycoder 阅读(1045) 评论(0) 推荐(0) 编辑
摘要: Write an algorithm to find the 'next' node(i.e. in-order successor) of a given node in a binary search tree. 这道题让我们寻找给定节点的中序后继节点,最简单的方法就是中序遍历tree, 并用一 阅读全文
posted @ 2018-06-18 08:22 infinitycoder 阅读(86) 评论(0) 推荐(0) 编辑
摘要: Implement a function to check if a binary tree is a binary search tree. 这道题很经典,让我们判断一棵树是不是二叉查找树。但是首先要确定一下二叉查找树的定义,比如leetcode 98题中的定义左<根<右就可以直接通过判断中序遍历 阅读全文
posted @ 2018-06-16 05:26 infinitycoder 阅读(81) 评论(0) 推荐(0) 编辑
摘要: Implement a function to check if a binary tree is balanced. For the purpose of this question, a balanced tree is defined to be a tree such that the he 阅读全文
posted @ 2018-06-16 00:50 infinitycoder 阅读(173) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, design an algorithm which creates a linked list of all the nodes at each depth.(e.g., if you have a tree with depth D, you'll hav 阅读全文
posted @ 2018-06-16 00:06 infinitycoder 阅读(257) 评论(0) 推荐(0) 编辑
摘要: Given a sorted(increasing order) array with unique integer elements, write an algorithm to create a binary search tree with minimal height. 这道题给了我们一个从 阅读全文
posted @ 2018-06-15 06:17 infinitycoder 阅读(140) 评论(0) 推荐(0) 编辑
摘要: Given a directed graph, design an algorithm to find out whether there is a route between nodes. 这道题让我们判断给定的一个图的任意两个节点间是否有路径。首先我们要知道如何定义一个图, 一般最常用的两种表示 阅读全文
posted @ 2018-06-15 04:36 infinitycoder 阅读(246) 评论(0) 推荐(0) 编辑