第一次动手写二叉树的,有点小激动,64行的if花了点时间,上传leetcode一次点亮~~~

 /* inorder traversal binary tree */
#include <stdio.h>
#include <stdlib.h> struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
}; int* inorderTraversal(struct TreeNode* root, int* returnSize); int main() { struct TreeNode n1, n2, n3;
n1.val = 1;
n1.left = NULL;
n1.right = &n2;
/* */
n2.val = 2;
n2.left = &n3;
n2.right = NULL;
/* */
n3.val = 3;
n3.left = NULL;
n3.right = NULL;
int returnSize = 0;
int *a = inorderTraversal(&n1, &returnSize); int i=0;
for(i=0; i<returnSize; i++)
printf("%d ", a[i]); printf("\n"); } /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
/**
* Return an array of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/
int* inorderTraversal(struct TreeNode* root, int* returnSize) { struct TreeNode **stack = (struct TreeNode **) malloc (sizeof(struct TreeNode *) * 1000); /* store node not access at present */
int *result = (int *) malloc(sizeof(int) * 1000);
int count = 0;
stack[0] = root; /* first node */
struct TreeNode* curr;
int top = 0; /* element number in stack */ while(top != -1 ) {
curr = stack[top]; /* get stack top element */ if(curr == NULL) { /* if current element is null */
while(top != -1 && curr == NULL)
curr = stack[--top];
if(top == -1 || curr == NULL)
break;
else {
result[count++] = curr->val;
stack[top] = curr->right;
continue;
}
}
if(curr->left != NULL)
stack[++top] = curr->left; if(curr->left == NULL) { /* if left subtree is NULL, then we need to access middle node */
result[count++] = curr->val;
stack[top] = curr->right;
}
}
*returnSize = count;
return result;
}

LeetCode:二叉树的非递归中序遍历的更多相关文章

  1. Python实现二叉树的非递归中序遍历

    思路: 1. 使用一个栈保存结点(列表实现): 2. 如果结点存在,入栈,然后将当前指针指向左子树,直到为空: 3. 当前结点不存在,则出栈栈顶元素,并把当前指针指向栈顶元素的右子树: 4. 栈不为空 ...

  2. java创建二叉树并实现非递归中序遍历二叉树

    java创建二叉树并递归遍历二叉树前面已有讲解:http://www.cnblogs.com/lixiaolun/p/4658659.html. 在此基础上添加了非递归中序遍历二叉树: 二叉树类的代码 ...

  3. java建立二叉树,递归/非递归先序遍历,递归/非递归中序遍历,层次遍历

    import java.util.LinkedList; import java.util.Scanner; import java.util.Stack; //structure of binary ...

  4. Python实现二叉树的非递归先序遍历

    思路: 1. 使用列表保存结果: 2. 使用栈(列表实现)存储结点: 3. 当根结点存在,保存结果,根结点入栈: 4. 将根结点指向左子树: 5. 根结点不存在,栈顶元素出栈,并将根结点指向栈顶元素的 ...

  5. LeetCode 94 | 基础题,如何不用递归中序遍历二叉树?

    今天是LeetCode专题第60篇文章,我们一起来看的是LeetCode的94题,二叉树的中序遍历. 这道题的官方难度是Medium,点赞3304,反对只有140,通过率有63.2%,在Medium的 ...

  6. 【LeetCode-面试算法经典-Java实现】【145-Binary Tree Postorder Traversal(二叉树非递归后序遍历)】

    [145-Binary Tree Postorder Traversal(二叉树非递归后序遍历)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a bin ...

  7. LeetCode二叉树的前序、中序、后序遍历(递归实现)

    本文用递归算法实现二叉树的前序.中序和后序遍历,提供Java版的基本模板,在模板上稍作修改,即可解决LeetCode144. Binary Tree Preorder Traversal(二叉树前序遍 ...

  8. [LeetCode]105. 从前序与中序遍历序列构造二叉树(递归)、108. 将有序数组转换为二叉搜索树(递归、二分)

    题目 05. 从前序与中序遍历序列构造二叉树 根据一棵树的前序遍历与中序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 题解 使用HashMap记录当前子树根节点在中序遍历中的位置,方便每次 ...

  9. [leetcode] 二叉树的前序,中序,后续,层次遍历

    前序遍历 [144] Binary Tree Preorder Traversal 递归遍历 使用递归,先保存父节点的值,再对左子树进行遍历(递归),最后对右子树进行遍历(递归) vector< ...

随机推荐

  1. HDU-3718 Similarity

    题目只有26个字母,所以我们新建一个二分图,v[i][j]表示字母i对应字母j时能成功匹配的个数,给这个边矩阵v求个最大匹配就是答案. #include <cstdlib> #includ ...

  2. tp90和tp99是指什么性能指标

    原文: https://www.zhihu.com/question/41110088 https://www.google.com.hk/#safe=strict&q=tp50+tp90 T ...

  3. AC日记——[Sdoi2013]森林 bzoj 3123

    3123: [Sdoi2013]森林 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 3216  Solved: 944[Submit][Status] ...

  4. AC日记——Destroying The Graph poj 2125

    Destroying The Graph Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8356   Accepted: 2 ...

  5. react css module

    <div className={style['content-warp']}></div> <div className={style.search}></d ...

  6. 树(弱化版)(lca)

    3306: 树 时间限制: 10 Sec  内存限制: 256 MB 题目描述 给定一棵大小为 n 的有根点权树,支持以下操作:  • 换根  • 修改点权      • 查询子树最小值 输入 第一行 ...

  7. BZOJ1001[BeiJing2006]狼抓兔子最小割網絡流

    Description 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的, 而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一 ...

  8. FireDAC 出现Variable length column[*] overflow. Value length - [80], column maximum length

    FireDAC 出现Variable length column[*] overflow. Value length - [80], column maximum length FireDAC的 TF ...

  9. A Good User Interface

    has high conversion rates and is easy to use. In other words, it's nice to both the business side as ...

  10. 如果当前地图文档中有独立的Table,通过Engine如何获取该Table?

    将IMap转为ITableCollection,通过ITableCollection.get_Table(int index);来获取该Table