LeetCode:二叉树的非递归中序遍历

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

  1 /* inorder traversal binary tree */
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4
  5
  6 struct TreeNode {
  7     int val;
  8     struct TreeNode *left;
  9     struct TreeNode *right;
 10 };
 11
 12 int* inorderTraversal(struct TreeNode* root, int* returnSize);
 13
 14 int main() {
 15
 16     struct TreeNode n1, n2, n3;
 17     n1.val = 1;
 18     n1.left = NULL;
 19     n1.right = &n2;
 20     /*  */
 21     n2.val = 2;
 22     n2.left = &n3;
 23     n2.right = NULL;
 24     /*  */
 25     n3.val = 3;
 26     n3.left = NULL;
 27     n3.right = NULL;
 28     int returnSize = 0;
 29     int *a = inorderTraversal(&n1, &returnSize);
 30
 31     int i=0;
 32     for(i=0; i<returnSize; i++)
 33         printf("%d ", a[i]);
 34
 35     printf("\n");
 36
 37 }
 38
 39
 40 /**
 41  * Definition for a binary tree node.
 42  * struct TreeNode {
 43  *     int val;
 44  *     struct TreeNode *left;
 45  *     struct TreeNode *right;
 46  * };
 47  */
 48 /**
 49  * Return an array of size *returnSize.
 50  * Note: The returned array must be malloced, assume caller calls free().
 51  */
 52 int* inorderTraversal(struct TreeNode* root, int* returnSize) {
 53
 54     struct TreeNode  **stack = (struct TreeNode **) malloc (sizeof(struct TreeNode *) * 1000); /* store node not access at present */
 55     int *result = (int *) malloc(sizeof(int) * 1000);
 56     int count = 0;
 57     stack[0] = root;           /* first node */
 58     struct TreeNode* curr;
 59     int top = 0;                /* element number in stack */
 60
 61     while(top != -1 ) {
 62         curr = stack[top];    /* get stack top element */
 63
 64         if(curr == NULL) {     /* if current element is null */
 65             while(top != -1 && curr == NULL)
 66                 curr = stack[--top];
 67             if(top == -1 || curr == NULL)
 68                 break;
 69             else {
 70                 result[count++] = curr->val;
 71                 stack[top] = curr->right;
 72                 continue;
 73             }
 74         }
 75         if(curr->left != NULL)
 76             stack[++top] = curr->left;
 77
 78         if(curr->left == NULL) {  /* if left subtree is NULL, then we need to access middle node */
 79             result[count++] = curr->val;
 80             stack[top] = curr->right;
 81         }
 82     }
 83     *returnSize = count;
 84     return result;
 85 }
时间: 2024-10-09 04:17:09

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

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

思路: 1. 使用一个栈保存结点(列表实现): 2. 如果结点存在,入栈,然后将当前指针指向左子树,直到为空: 3. 当前结点不存在,则出栈栈顶元素,并把当前指针指向栈顶元素的右子树: 4. 栈不为空,循环2.3部. 代码如下,解决了leetcode94. Binary Tree Inorder Traversal: # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): #

完全二叉树的链式存储结构的转化 &amp; 非递归中序遍历二叉树

1 /* 2 * 二叉树 3 * 4 * (将完全二叉树的数组形式改为链表形式) 5 * 6 * 1 7 * 2 3 8 * 4 5 6 7 9 * 8 10 * 11 */ 12 #include <iostream> 13 #define MAX 10 14 using namespace std; 15 16 typedef struct btnode{ 17 int data; 18 struct btnode * lchild; 19 struct btnode * rchild;

94.Binary Tree Inorder Traversal(非递归中序遍历)

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 istrivial, could you do it iteratively? confused what "{1,#,2,3}" means? > rea

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

思路: 1. 使用列表保存结果: 2. 使用栈(列表实现)存储结点: 3. 当根结点存在,保存结果,根结点入栈: 4. 将根结点指向左子树: 5. 根结点不存在,栈顶元素出栈,并将根结点指向栈顶元素的右子树: 6. 重复步骤3-6,直到栈空. LeetCode: 144. Binary Tree Preorder Traversal # Definition for a binary tree node. # class TreeNode(object): # def __init__(self

数据结构复习之二叉树的非递归先序,中序,后序遍历

#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<stack> using namespace std; struct Tree{ int x; Tree *lchild, *rchild; Tree(){ lchild = rchild = NULL; } }; typedef Tree* pT; void buildT(pT &

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

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

非递归后序遍历二叉树(1)

1 void postOrder3(BinTree *root) //非递归后序遍历 2 { 3 stack<BinTree*> s; 4 BinTree *cur; //当前结点 5 BinTree *pre=NULL; //前一次访问的结点 6 s.push(root); 7 while(!s.empty()) 8 { 9 cur=s.top(); 10 if((cur->lchild==NULL&&cur->rchild==NULL)|| 11 (pre!=N

二叉树(2)----中序遍历,递归和非递归实现

1.二叉树定义: typedef struct BTreeNodeElement_t_ { void *data; } BTreeNodeElement_t; typedef struct BTreeNode_t_ { BTreeNodeElement_t *m_pElemt; struct BTreeNode_t_ *m_pLeft; struct BTreeNode_t_ *m_pRight; } BTreeNode_t; 2.中序遍历 定义:首先访问左子树,然后访问根节点,最后访问右子树

[LeetCode]105. 从前序与中序遍历序列构造二叉树(递归)

题目 根据一棵树的前序遍历与中序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 题解 递归 递归传参:该子树对应的前序遍历和中序遍历(用开始结束指针表示即可) 确定每层:new当前子树根节点,左右孩子分别赋值为递归的返回值 递归结束条件:返回当前子树根节点 使用HashMap记录当前子树根节点在中序遍历中的位置,方便每次查找. 利用左子树节点数量查找idx 代码 /** * Definition for a binary tree node. * public class TreeNo