【先序遍历】【中序遍历】中序遍历非递归遍历算法:遇到一个结点,就把它压栈,并去遍历它的左子树;当左子树遍历结束后,从栈顶弹出这个结点并访问它;然后按其右指针再去中序遍历该结点的右子树;【后续遍历】【层序遍历】
分类:
其他好文 时间:
2015-02-03 12:30:14
阅读次数:
173
题目链接:https://oj.leetcode.com/problems/binary-tree-inorder-traversal/
题目:
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3},
...
分类:
其他好文 时间:
2015-02-02 23:21:23
阅读次数:
294
package main
//表达式
//author:Xiong Chuan Liang
//date:2015-2-2
import (
"fmt"
"github.com/xcltapestry/xclpkg/algorithm"
"strconv"
"errors"
)
func main(){
// 中序表达式 后序表达式
// a+b...
分类:
编程语言 时间:
2015-02-02 23:10:39
阅读次数:
278
Given a binary tree, return the inorder traversal of itsnodes' values.
For example:
Given binary tree {1,#,2,3},
1
2
/
3
return [1,3,2].
Note: Recursive solution istri...
分类:
其他好文 时间:
2015-01-30 22:53:42
阅读次数:
203
#include #include //*****二叉树的二叉链表存储表示*****// typedef struct BiNode { char data; struct BiNode *lchild, *rchild; }BiNode, *BiTree; //...
分类:
编程语言 时间:
2015-01-30 22:15:27
阅读次数:
172
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
解题思路:采用中序排列的方法递归地决定每个结点的数值;
#include
#include
#include
using namespace std;
//Definition...
分类:
其他好文 时间:
2015-01-30 16:00:24
阅读次数:
131
原题地址中序遍历二叉树,递归展开。别忘了将left设为NULL,如果忘掉的话可能报Runtime Error,而且这个RE很难查出原因。代码: 1 TreeNode *solve(TreeNode *root) { 2 if (!root) return NULL; 3 4 ...
分类:
其他好文 时间:
2015-01-30 14:31:21
阅读次数:
121
原题地址中序遍历二叉树,如果出现逆序对,则说明不是合法BST代码: 1 bool isValidBST(TreeNode *root) { 2 stack st; 3 int last = 0; 4 bool hasLast = false; 5 ...
分类:
其他好文 时间:
2015-01-29 19:01:22
阅读次数:
154
原题地址递归代码谁都会,当然是写非递归代码了。最基本的写法是用一个特殊栈同时保存节点以及节点的左孩子、右孩子是否遍历过。这种思路比较简单,跟递归写法一样很好理解。前序、中序、后序的遍历写法类似。还有一种更"屌"的写法,只需要使用普通栈即可,不需要保存左孩子、右孩子是否遍历过。基本思路是:1. 只要当...
分类:
其他好文 时间:
2015-01-29 11:50:43
阅读次数:
96
原题地址有人些的做法是判断中序遍历序列是否是回文串,一开始我觉得挺有道理,但是琢磨了一阵觉得没那么简单。比如下面这个树: 1 / 1 / 1中序遍历序列是"111",虽然是回文串但这棵树明显不是对称的。如果要是把NULL也算进去呢?还是上面...
分类:
其他好文 时间:
2015-01-26 14:55:27
阅读次数:
165