3th Validate Binary Search Tree
来源:互联网 发布:下载itools软件 编辑:程序博客网 时间:2024/06/15 21:47
用递归做得,遇到的坑是用cygwin带的gcc编译,总是遇到各种莫名其妙的段溢出。。。珍爱生命,远离cygwin。。
#include "stdio.h"#include "stdlib.h"#include "string.h" //Definition for a binary tree node. struct TreeNode { char val; struct TreeNode *left; struct TreeNode *right; int index_a; }; int index_t = 0,length=0; int make_tree(struct TreeNode *root,char *str) { static int index_p = 0 , cou_t = 0; static char buf[10] ; int i=0 , rc; root = (struct TreeNode *)malloc(sizeof(struct TreeNode )); root->val = str[index_p]; buf[cou_t] = root->val; printf("val %c\n", root->val); index_p++; cou_t++; if(root->val == '@') { printf("return @\n"); return 1; } rc = make_tree(root->left,str); if(!rc) return rc; rc = make_tree(root->right,str); if(!rc) return rc; printf("tree root is %c\n",root->val); printf("dump buf[cou_t-3] %c buf[cou_t-2] %c buf[cou_t-1] %c\n",buf[cou_t-3],buf[cou_t-2],buf[cou_t-1]); if((buf[cou_t-1]=='@')||(buf[cou_t-2]=='@')||(buf[cou_t-3]=='@')) { cou_t -= 2; return 1; } if((buf[cou_t-3]<buf[cou_t-2])||(buf[cou_t-3]>buf[cou_t-1])) { printf("err!\n"); return 0; } cou_t -= 2; return 1; } int get_input(char *str) { gets(str); puts(str); //printf("\nlens is %d\n", strlen(str)); length = strlen(str); return strlen(str); } void main(void) { char str[30]; int rc; printf("hello cygwin\n"); struct TreeNode *root; get_input(str); rc = make_tree(root , str); if(rc) printf("good\n"); else printf("bad\n"); }
阅读全文
0 0
- 3th Validate Binary Search Tree
- Validate Binary Search Tree
- Validate Binary Search Tree
- Validate Binary Search Tree
- Validate Binary Search Tree
- Validate Binary Search Tree
- Validate Binary Search Tree
- Validate Binary Search Tree
- Validate Binary Search Tree
- Validate Binary Search Tree
- Validate Binary Search Tree
- Validate Binary Search Tree
- Validate Binary Search Tree
- Validate Binary Search Tree
- Validate Binary Search Tree
- Validate Binary Search Tree
- Validate Binary Search Tree
- Validate Binary Search Tree
- 欢迎使用CSDN-markdown编辑器
- AOP的应用
- httplib, httplib2, urllib, requests 区别
- SQL Server基础
- Spark源码系列(五)分布式缓存
- 3th Validate Binary Search Tree
- Spark源码系列(六)Shuffle的过程解析
- 【C++】动态规划
- 如何搜索自己博客内的文章
- Spark源码系列(七)Spark on yarn具体实现
- C/C++运算符优先级
- Spark源码系列(八)Spark Streaming实例分析
- redis初见
- Hibernate第四天