【初级算法:树】

 

 

二叉树的深度,递归求

 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     
13     int maxDepth(TreeNode* root) {
14         int depth=0,high=0;
15         dfs(root,depth,high);
16         return depth;
17     }
18     void dfs(TreeNode* root,int &depth,int high){
19         if(root==NULL) return;
20         high++;
21         if(high>depth) depth=high;
22         dfs(root->left,depth,high);
23         dfs(root->right,depth,high);
24         high--;
25     }
26 };

 

二叉搜索树的判断:

 1 class Solution {
 2 public:
 3     bool isValidBST(TreeNode* root) {
 4         bool res=true;
 5         vector<int> vals;
 6         dfs(root,vals);
 7         for(int i=1;i<vals.size();i++){
 8             if(vals[i-1]>=vals[i]){
 9                 res=false;break;
10             }
11         }
12         return res;
13     }
14     void dfs(TreeNode* root,vector<int> &vals){
15         if(root==NULL) return;
16         dfs(root->left,vals);
17         vals.push_back(root->val);
18         dfs(root->right,vals);
19     }
20 };

 

对称二叉树:

 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 isSymmetric(TreeNode* root) {
13         bool res=true;
14         vector<int> vals;
15         dfs(root,vals);
16         int i=0;int j=vals.size()-1;
17         while(i<j){
18             if(vals[i]!=vals[j]){
19                 res=false;break;
20             }
21             i++;j--;
22         }
23         return res;
24     }
25     void dfs(TreeNode* root,vector<int> &vals){
26         if(root==NULL) return;
27         dfs(root->left,vals);
28         vals.push_back(root->val);
29         dfs(root->right,vals);
30     }
31 };

二叉树层次遍历:

 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     vector<vector<int>> levelOrder(TreeNode* root) {
13         vector<vector<int>> res;
14         if (root==NULL) return res;
15         queue<TreeNode*> q;
16         q.push(root);
17         while(!q.empty()){
18             vector<int> level;
19             int len=q.size();
20             for(int i=0;i<len;i++){
21                 TreeNode *front=q.front();
22                 level.push_back(front->val);
23                 q.pop();
24                 if(front->left) q.push(front->left);
25                 if(front->right) q.push(front->right);
26             }
27             res.push_back(level);
28         }
29         return res;
30     }
31 };

 

posted @ 2019-03-09 23:36  Joel_Wang  阅读(162)  评论(0编辑  收藏  举报