二叉树相关题目

LeetCode 98. Validate Binary Search Tree(判断是否是二叉搜索树)

思路1:对树进行深度优先遍历,遍历到某个节点是判断该节点的左子树是否为二叉搜索树,右子树是否为二叉搜索树,左子树的最大值是否小于当前节点的值,右子树的最小值是否大于当前节点的值。

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     bool isValidBST(TreeNode* root) {
13         if(root == NULL) return true;
14         int maxv, minv;
15         return dfs(root, maxv, minv);
16     }
17     
18     bool dfs(TreeNode* root, int &maxv, int &minv){
19         maxv = minv = root->val;
20         if(root->left){
21             int nowMaxv, nowMinv;
22             if(!dfs(root->left, nowMaxv, nowMinv)) 
23                 return false;
24             if(nowMaxv >= root->val) 
25                 return false;
26             maxv = max(maxv, nowMaxv);
27             minv = min(minv, nowMinv);
28         }
29         if(root->right){
30             int nowMaxv, nowMinv;
31             if (!dfs(root->right, nowMaxv, nowMinv))
32                 return false;
33             if (nowMinv <= root->val)
34                 return false;
35             maxv = max(maxv, nowMaxv);
36             minv = min(minv, nowMinv);
37         }
38         return true;
39     }
40 };

思路2:对树进行中序遍历,遍历的过程中判断当前值是否大于前一个值。

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     bool isValidBST(TreeNode* root) {
13         stack<TreeNode*> st;
14         int pre_val;
15         bool first_flag = true;
16         while(!st.empty() || root!=NULL){
17             while(root!=NULL){
18                 st.push(root);
19                 root = root->left;
20             }
21             root = st.top();
22             st.pop();
23             if(first_flag)
24                 first_flag = false;
25             else if(root->val <= pre_val) 
26                     return false;
27             pre_val = root->val;
28             root = root->right;
29         }
30         return true;
31     }
32 };

2. LeetCode 101. Symmetric Tree(对称的二叉树)

递归实现

 1 class Solution {
 2 public:
 3     bool isSymmetric(TreeNode* root) {
 4         if(root == NULL) return true;
 5         return judge(root->left, root->right);
 6     }
 7     
 8     bool judge(TreeNode* root1, TreeNode* root2){
 9         if(root1==NULL && root2==NULL) return true;
10         if(root1==NULL || root2==NULL) return false;
11         if(root1->val != root2->val) return false;
12         return judge(root1->left, root2->right) &&
13                judge(root1->right, root2->left);
14      }
15 };
View Code

非递归实现

 1 class Solution {
 2 public:
 3     bool isSymmetric(TreeNode* root) {
 4         if(root==NULL) return true;
 5         stack<TreeNode* > lstack, rstack;
 6         TreeNode* lt = root->left, *rt = root->right;
 7         while(lt || rt || lstack.size()){
 8             // 左右子树都不为空
 9             while(lt && rt){
10                 lstack.push(lt), rstack.push(rt);
11                 lt = lt->left, rt=rt->right;
12             }
13             // 一个为空,一个不为空
14             if(lt || rt) return false;
15             // 左右子树都为空
16             lt = lstack.top(), rt = rstack.top();
17             lstack.pop(), rstack.pop();
18             if(lt->val != rt->val) return false;
19             lt = lt->right, rt=rt->left;
20         }
21         return true;
22     }
23 };
View Code

 3. LeetCode 297 Serialize and Deserialize Binary Tree(二叉树的序列化)

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Codec {
11 public:
12 
13     // Encodes a tree to a single string.
14     string serialize(TreeNode* root) {
15         string ans = "";
16         pre_order(root, ans);
17         return ans;
18         
19     }
20 
21     // Decodes your encoded data to tree.
22     TreeNode* deserialize(string data) {
23         int cur=0;
24         return decode(cur, data);
25     }
26     
27     void pre_order(TreeNode* root, string& s){
28         if(root == NULL){
29             s += ",";
30             return;
31         }
32         s += to_string(root->val) + ",";
33         pre_order(root->left, s);
34         pre_order(root->right, s);
35     }
36     
37     TreeNode* decode(int &cur, string &data){
38         if(data[cur] == ','){
39             cur++;
40             return NULL;
41         }
42         int next = data.find(',', cur);
43         int val = stoi(data.substr(cur, next-cur));
44         TreeNode* node = new TreeNode(val);
45         cur = next + 1;
46         node->left = decode(cur, data);
47         node->right = decode(cur, data);
48         return node;
49     }
50 };
51 
52 // Your Codec object will be instantiated and called as such:
53 // Codec codec;
54 // codec.deserialize(codec.serialize(root));
View Code

 

posted @ 2019-07-22 17:20  Longice  阅读(226)  评论(0编辑  收藏  举报