码迷,mamicode.com
首页 > 其他好文 > 详细

Chap4: question: 19 - 28

时间:2014-05-01 12:41:11      阅读:416      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   tar   width   strong   color   string   int   art   

19. 二叉树的镜像(递归)

 即:交换所有节点的左右子树。从下往上 或 从上往下 都可以。

 mamicode.com,码迷

 20. 顺时针打印矩阵

mamicode.com,码迷
 1 #include <stdio.h>
 2 
 3 void printMatrix(int (*A)[1], int rows, int columns)
 4 {
 5     if(rows < 1 || columns < 1) return;
 6     int r1, r2, c1, c2;
 7     r1 = c1 = 0, r2 = rows-1, c2 = columns-1;
 8     while(r1 <= r2 && c1 <= c2) /* 打印结束时, r1 = r2+1, c1 = c2+1; */
 9     {
10         for(int i = c1; i <= c2 && r1 <= r2; ++i)
11             printf("%d ", A[r1][i]);
12         ++r1;
13         for(int i = r1; c1 <= c2 && i <= r2; ++i)
14             printf("%d ", A[i][c2]);      /*   要保证 c1 <= c2  */      
15         --c2;
16         for(int i = c2; i >= c1 && r1 <= r2; --i)
17             printf("%d ", A[r2][i]);
18         --r2;
19         for(int i = r2; i >= r1 && c1 <= c2; --i)
20             printf("%d ", A[i][c1]);
21         ++c1;
22     }
23     printf("\n");
24 }
25 int main()
26 {
27     int test1[3][3] = {{1, 2, 3},
28                        {4, 5, 6}, 
29                         {7, 8, 9}};
30     printMatrix(test1, 3, 3);
31 
32     int test2[1][3] = {1, 2, 3};
33     printMatrix(test2, 1, 3);
34 
35 /*  // first set int (*A)[1], then began called below. 
36     int test3[3][1] = {{1}, {2}, {3}};
37     printMatrix(test3, 3, 1);
38 
39     int test4[1][1] = {1};
40     printMatrix(test4, 1, 1);
41 */
42     return 0;
43 }
View Code

 mamicode.com,码迷     (mamicode.com,码迷

 21. 包含 min  函数的栈

 要求调用 min,pop,push,时间复杂度都是 O(1)。

mamicode.com,码迷
 1 #include <iostream>
 2 #include <stack>
 3 #include <cassert>
 4 #include <string>
 5 template<typename T> class Stack
 6 {
 7 public:
 8     void push(T value);
 9     void pop();
10     T min();
11 private:
12     std::stack<T> data;
13     std::stack<T> minData;
14 };
15 template<typename T> void Stack<T>::push(T value)
16 {
17     data.push(value);
18     if(minData.empty() || minData.top() >= value)
19         minData.push(value);
20     else
21         minData.push(minData.top());
22 }
23 template<typename T> void Stack<T>::pop()
24 {
25     assert(data.size() > 0 && minData.size() > 0);
26     data.pop();
27     minData.pop();
28 }
29 template<typename T> T Stack<T>::min()
30 {
31     return minData.top();
32 }
33 
34 int main()
35 {
36     Stack<char> st;
37     std::string numbers;
38     while(std::cin >> numbers)
39     {
40         for(size_t i = 0; i < numbers.length(); ++i) st.push(numbers[i]);
41         for(size_t i = 0; i < numbers.length(); ++i)
42         {
43             std::cout << "st.min(): " << st.min() << std::endl;
44             st.pop();
45         }
46     }
47     return 0;
48 }
View Code

mamicode.com,码迷mamicode.com,码迷

22. 根据栈的压入序列,判断一个序列是否是弹出序列。 

 mamicode.com,码迷mamicode.com,码迷

23. 从上往下打印二叉树

 Go:(Chap2: question: 1 - 10—>6. 重建二叉树—>二叉树的各种遍历)Link: http://www.cnblogs.com/liyangguang1988/p/3667443.html

24. 判断序列是否为二叉搜索树的后序遍历(递归)

note: 二叉搜索树和遍历序列一一对应。

例:后序序列 {3,5,4,6,11,13,12, 10, 8} ,可画出一颗二叉搜索树。

mamicode.com,码迷
 1 #include <iostream> 
 2 /* verify the seq which should be the postOrder of a Binary Search Tree  */
 3 bool postOrderOfBST(int sequence[], int len)
 4 {
 5     if(sequence == NULL || len == 0) return false;
 6     bool answer = false;
 7     int root = sequence[len-1];    /* value of root node */
 8 
 9     int leftLength = 0;
10     for(; leftLength < len-1; ++leftLength)
11     {
12         if(sequence[leftLength] > root) break;
13     }
14     /*  verify right-subtree, should big than root  */
15     for(int i = leftLength + 1; i < len -1; ++i)
16     {
17         if(sequence[i] < root) return false;
18     }
19 
20     int rightLength = len - 1 - leftLength;
21     bool left = true, right = true;
22     if(leftLength > 0)
23         bool left = postOrderOfBST(sequence, leftLength);
24     if(rightLength)
25         bool right = postOrderOfBST(sequence+leftLength, rightLength);
26     return (left && right);
27 }
28 
29 void printResult(bool v)
30 {
31     std::cout << "Is LRD of a BFS?  " ; 
32     if(v)  std::cout << "true" << std::endl;
33     else  std::cout << "false" << std::endl; 
34 }
35 int main() 
36 {
37     std::cout << "Test 1: ";
38     int test1[] = {3, 5, 4, 6, 11, 13, 12, 10, 8};
39     printResult(postOrderOfBST(test1, sizeof(test1) / 4)) ;
40 
41     std::cout << "Test 2: ";
42     int test2[] = {4, 2, 3}; 
43     printResult(postOrderOfBST(test2, sizeof(test2) / 4)); 
44         
45     std::cout << "Test 3: ";
46     int test3[] = {1};
47     printResult(postOrderOfBST(test3, sizeof(test3) / 4));
48 
49     return 0; 
50 } 
View Code

mamicode.com,码迷

 25. 二叉树中和为某一值的路径

 

 

 

 

 

 

 

 

Chap4: question: 19 - 28,码迷,mamicode.com

Chap4: question: 19 - 28

标签:style   blog   class   code   tar   width   strong   color   string   int   art   

原文地址:http://www.cnblogs.com/liyangguang1988/p/3701499.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!