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");  }


原创粉丝点击